题解 | #数值的整数次方#
数值的整数次方
https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00
class Solution {
public:
double f(double x,int y)
{
double base=x, r=1;
while(y)
{
if(y&1)
r=r*base;
base=base*base;
y>>=1;
}
return r;
}
double Power(double base, int exponent) {
if(exponent<0){
base=1/base;
exponent=-exponent;
}
return f(base,exponent);
}
};
快速幂模板题。
