-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower.cpp
More file actions
43 lines (40 loc) · 1.32 KB
/
Power.cpp
File metadata and controls
43 lines (40 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// 面试题10 斐波那契数列
class Solution {
public:
/// O**0没有意义 返回 0/1都可以
/// 注意处理 base == 0 && exponent <= 0 的情况
/// 使用快速幂运算加速
bool invalid_value = false;
double Power(double base, int exponent)
{
invalid_value = false;
if (base == 0.0 && exponent < 0) {
///对0取倒数会出错
invalid_value = true;
return 0.0;
}
/// 书上使用unsinged int来存储
/// 这样可以避免负整数转为正整数时的溢出
unsigned int abs_exponent = (unsigned int)exponent;
if (exponent < 0)
abs_exponent = (unsigned int)(-exponent);
double result = PowerWithUnsignedExponent(base, abs_exponent);
return exponent < 0 ? 1.0 / result : result;
}
double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
/// 快速幂运算
/// a**b = a**(b/2) * a**(b/2) b位偶数
/// a**b = a**(b/2) * a**(b/2) *a b位奇数
if (exponent == 0)
return 1;
if (exponent == 1)
return base;
double result = PowerWithUnsignedExponent(base, exponent / 2);
result *= result;
if (exponent & 0x1) {
result *= base;
}
return result;
}
};