-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_Power.cpp
More file actions
143 lines (113 loc) · 3.37 KB
/
Copy path16_Power.cpp
File metadata and controls
143 lines (113 loc) · 3.37 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#if 0
#include<iostream>
#include<cmath>
using namespace std;
#define eps 1e-6
double Power(double base, int exponent)
{
if(base - 0.0 < eps && base - 0.0 > -eps && exponent < 0)
return 0.0;
if(exponent == 0)
return 1.0;
if(exponent == 1)
return base;
double result = 1.0;
for(int i = 1; i <= abs(exponent); i++)
{
result *= base;
}
if(exponent < 0)
result = 1 / result;
return result;
}
#endif
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题16:数值的整数次方
// 题目:实现函数double Power(double base, int exponent),求base的exponent
// 次方。不得使用库函数,同时不需要考虑大数问题。
#include <iostream>
#include <cmath>
bool g_InvalidInput = false;
bool equal(double num1, double num2);
double PowerWithUnsignedExponent(double base, unsigned int exponent);
double Power(double base, int exponent)
{
g_InvalidInput = false;
if (equal(base, 0.0) && exponent < 0)
{
g_InvalidInput = true;
return 0.0;
}
unsigned int absExponent = (unsigned int) (exponent);
if (exponent < 0)
absExponent = (unsigned int) (-exponent);
double result = PowerWithUnsignedExponent(base, absExponent);
if (exponent < 0)
result = 1.0 / result;
return result;
}
/*
double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
double result = 1.0;
for (int i = 1; i <= exponent; ++i)
result *= base;
return result;
}
*/
double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
if (exponent == 0)
return 1;
if (exponent == 1)
return base;
double result = PowerWithUnsignedExponent(base, exponent >> 1);
result *= result;
if ((exponent & 0x1) == 1)
result *= base;
return result;
}
bool equal(double num1, double num2)
{
if ((num1 - num2 > -0.0000001) && (num1 - num2 < 0.0000001))
return true;
else
return false;
}
// ====================测试代码====================
void Test(const char* testName, double base, int exponent, double expectedResult, bool expectedFlag)
{
double result = Power(base, exponent);
if (equal(result, expectedResult) && g_InvalidInput == expectedFlag)
std::cout << testName << " passed" << std::endl;
else
std::cout << testName << " FAILED" << std::endl;
}
int main(int argc, char* argv[])
{
// 底数、指数都为正数
Test("Test1", 2, 3, 8, false);
// 底数为负数、指数为正数
Test("Test2", -2, 3, -8, false);
// 指数为负数
Test("Test3", 2, -3, 0.125, false);
// 指数为0
Test("Test4", 2, 0, 1, false);
// 底数、指数都为0
Test("Test5", 0, 0, 1, false);
// 底数为0、指数为正数
Test("Test6", 0, 4, 0, false);
// 底数为0、指数为负数
Test("Test7", 0, -4, 0, true);
return 0;
}