AcWing 27. 数值的整数次方(C++)- 模拟
题目链接:https://www.acwing.com/problem/content/description/26/
题目如下:
class Solution {
public:
double Power(double base, int exponent) {
double result=1.0;
long long n=abs((long long)(exponent));//令其取最大类型,并其绝对值
while(n){
if(n&1) result*=base;//取最后一位
base*=base;
n>>=1;//去除最后一位
}
if(exponent<0) result=1/result;
return result;
}
};
还没有评论,来说两句吧...