题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
1.math。pow()考查点肯定不在这
import java.lang.Math;
public class Solution {
public double Power(double base, int exponent) {
return Math.pow(base,exponent);
}
}
2.
public class Solution {
public double Power(double base, int exponent) {
if(exponent == 0) return 1.0;
if(exponent == 1) return base;
double result = 1.0;
if(exponent > 0) {
for(int i = 0; i < exponent; i++) {
result *= base;
}
} else {
for(int i = 0; i < (0-exponent); i++) {
result *= base;
}
result = 1/result;
}
return result;
}
}