题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
public class Solution {
public double Power(double base, int exponent){
if(exponent == 0){
return 1;
}
if(exponent < 0) {
int temp = - exponent;
double result = recursion(base, temp);
result = 1/result;
return result;
}else {
double result = recursion(base, exponent);
return result;
}
}
private double recursion(double base, int exponent) {
if(exponent == 1) {
return base;
}
if(exponent % 2 == 0) {
double temp = recursion(base,exponent/2);
temp *= temp;
return temp;
}else {
double temp = recursion(base,exponent/2);
temp *= temp * base;
return temp;
}
}
}