位运算第三题 知道次方是什么意思就行了 先都变为正,再算次方,如果小于0,那就是倒数
class Solution {
public:
double Power(double base, int exponent) {
double ans=1;
if(exponent == 0)
return ans;
int expabs=abs(exponent);
while (expabs>=1)
{
ans*=base;
expabs--;
}
if(exponent>0)
return ans;
else
return 1/ans;
}
};
public:
double Power(double base, int exponent) {
double ans=1;
if(exponent == 0)
return ans;
int expabs=abs(exponent);
while (expabs>=1)
{
ans*=base;
expabs--;
}
if(exponent>0)
return ans;
else
return 1/ans;
}
};