给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
public class Solution {
public double PowerPositive(double base, int exponent) {
if(exponent==0) return 1;
if(exponent==1) return base;
double muti = Power(base,exponent/2);
muti = muti * muti;
if(exponent%2==1){
muti = muti * base;
}
return muti;
}
public double Power(double base, int exponent) {
int ex = Math.abs(exponent);
double re = PowerPositive(base,ex);
if(exponent<0){
re = 1 / re;
}
return re;
}
}
网友评论