The sword refers to Offer--the integer power of the value--java

Topic description :
Given a float base of type double and an integer exponent of type int. Find the exponent power of base.

Problem- solving idea:
through loop iteration, realize the multiplication of n numbers, don't forget the case of negative numbers

代码如下:
public class Solution { public double Power(double base, int exponent) { double a=1.000000; if(exponent>=0){ for(int i=0;i<exponent;i++){ a=a*base; }





}else{ for(int i=0;i>exponent;i–){ a=a*base; } a=1/a; } return a; } }







operation result:
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324146607&siteId=291194637