方式一:只需要考虑简单的几种特殊情况,其余情况累乘
public double Power(double base, int exponent) {
if(base==0){
if(exponent==0){
return 1;
}
return 0;
}
if (exponent == 0) {
return 1;
} else if (exponent < 0) {
double res = 1;
for (int i = 0; i < -exponent; i++) {
res *= (1.0 / base);
}
return res;
} else {
double res = 1;
for (int i = 0; i < exponent; i++) {
res *= base;
}
return res;
}
}
方式二:调用方法Math.pow(double a, double b)
public double Power(double base, int exponent) {
return Math.pow(base,exponent);
}
网友评论