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