-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path面试题11之数值的整数次方_Power.cpp
114 lines (91 loc) · 2.4 KB
/
面试题11之数值的整数次方_Power.cpp
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
// Power.cpp : Defines the entry point for the console application.
//
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛
#include "stdafx.h"
#include <math.h>
#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(double base, int exponent, double expectedResult, bool expectedFlag)
{
double result = Power(base, exponent);
if(abs(result - expectedResult) < 0.00000001
&& g_InvalidInput == expectedFlag)
printf("Test passed.\n");
else
printf("Test failed.\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
// 底数、指数都为正数
printf("Test1 begins.\n");
Test(2, 3, 8, false);
// 底数为负数、指数为正数
printf("Test2 begins.\n");
Test(-2, 3, -8, false);
// 指数为负数
printf("Test3 begins.\n");
Test(2, -3, 0.125, false);
// 指数为0
printf("Test4 begins.\n");
Test(2, 0, 1, false);
// 底数、指数都为0
printf("Test5 begins.\n");
Test(0, 0, 1, false);
// 底数为0、指数为正数
printf("Test6 begins.\n");
Test(0, 4, 0, false);
// 底数为0、指数为负数
printf("Test7 begins.\n");
Test(0, -4, 0, true);
return 0;
}