实现?pow(x,?n)?,即计算?x
?的整数?n
?次幂函数(即,xn
?)。
示例 1:
输入:x = 2.00000, n = 10 输出:1024.00000
示例 2:
输入:x = 2.10000, n = 3 输出:9.26100
示例 3:
输入:x = 2.00000, n = -2 输出:0.25000 解释:2-2 = 1/22 = 1/4 = 0.25
class Solution {
public:
double Power_builer(double x,long long n)
{
if(n==0)
return 1;
auto val=Power_builer(x,n/2);
if(n%2==0)
{
return val*val;
}
else
{
return val*val*x;
}
}
double myPow(double x, int n) {
if(x==1)
return 1;
if(n==0)
return 1;
long long m=n;
bool sig=false;
if(n<0)
{
sig =true;
m=-1*m;
}
auto val=Power_builer(x,m);
if(sig)
return 1/val;
else
return val;
}
};