2024-03-19 02:45:09 +08:00

35 lines
1.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

double myPow(double x, int n) {
double ret = 1;
for (int i = n; i != 0; i = i / 2) {
if (i % 2 != 0) ret *= x;
x *= x;
}
return n > 0 ? ret : 1 / ret;
}
/*
double myPow(double x, int n) {
//如果n为零表明幂数为0直接返回1
if(n == 0)
return 1;
//如果n为零表明幂数为0直接返回x
if(n == 1)
return x;
//p值相当于将幂数折半减少重复计算
int p=n/2;
//如果n为负数那么将x值取其倒数然后再将p的值乘以-1这样就相当于
//求x倒数的n次幂你为正数
if(n < 0) {
//p值改为正数
p=-p;
//x值取其倒数
x = 1 / x;
}
double res = myPow(x, p);
//为了提高时间复杂度避免重复计算可以将n不断除二来减少计算量
//当n为32时除二就为16那么只需要将x^16与自身相乘即可若n为奇数
//那么只需要再乘以x即可
if(n % 2 == 0)
return res * res;
return res * res * x;
}
*/