-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path64_Accumulate.cpp
More file actions
202 lines (165 loc) · 4.14 KB
/
64_Accumulate.cpp
File metadata and controls
202 lines (165 loc) · 4.14 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************
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——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题64:求1+2+…+n
// 题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case
// 等关键字及条件判断语句(A?B:C)。
#include <cstdio>
// ====================方法一====================
class Temp
{
public:
Temp() { ++ N; Sum += N; }
static void Reset() { N = 0; Sum = 0; }
static unsigned int GetSum() { return Sum; }
private:
static unsigned int N;
static unsigned int Sum;
};
unsigned int Temp::N = 0;
unsigned int Temp::Sum = 0;
unsigned int Sum_Solution1(unsigned int n)
{
Temp::Reset();
Temp *a = new Temp[n];
delete []a;
a = NULL;
return Temp::GetSum();
}
// ====================方法二====================
class A;
A* Array[2];
class A
{
public:
virtual unsigned int Sum (unsigned int n)
{
return 0;
}
};
class B: public A
{
public:
virtual unsigned int Sum (unsigned int n)
{
return Array[!!n]->Sum(n-1) + n; //把数值变量n转换为bool类型,对n连续做两次反运算,那么非0的n转换为true,0转换为false
}
};
int Sum_Solution2(int n)
{
A a;
B b;
Array[0] = &a;
Array[1] = &b;
int value = Array[1]->Sum(n);
return value;
}
// ====================方法三====================
typedef unsigned int (*fun)(unsigned int);
unsigned int Solution3_Teminator(unsigned int n)
{
return 0;
}
unsigned int Sum_Solution3(unsigned int n)
{
static fun f[2] = {Solution3_Teminator, Sum_Solution3};
return n + f[!!n](n - 1);
}
// ====================方法四====================
template <unsigned int n> struct Sum_Solution4
{
enum Value { N = Sum_Solution4<n - 1>::N + n};
};
template <> struct Sum_Solution4<1>
{
enum Value { N = 1};
};
template <> struct Sum_Solution4<0>
{
enum Value { N = 0};
};
// ====================方法五====================
unsigned int Sum_Solution5(unsigned int n)
{
unsigned int sum = n;
//bool b = (n == 0) || (sum += Sum_Solution5(n - 1)) > 0;
bool b = (n > 0) && (sum += Sum_Solution5(n - 1)) > 0;
return sum;
}
// ====================测试代码====================
void Test(int n, int expected)
{
printf("Test for %d begins:\n", n);
if(Sum_Solution1(n) == expected)
printf("Solution1 passed.\n");
else
printf("Solution1 failed.\n");
if(Sum_Solution2(n) == expected)
printf("Solution2 passed.\n");
else
printf("Solution2 failed.\n");
if(Sum_Solution3(n) == expected)
printf("Solution3 passed.\n");
else
printf("Solution3 failed.\n");
if(Sum_Solution5(n) == expected)
printf("Solution5 passed.\n");
else
printf("Solution5 failed.\n");
}
void Test1()
{
const unsigned int number = 1;
int expected = 1;
Test(number, expected);
if(Sum_Solution4<number>::N == expected)
printf("Solution4 passed.\n");
else
printf("Solution4 failed.\n");
}
void Test2()
{
const unsigned int number = 5;
int expected = 15;
Test(number, expected);
if(Sum_Solution4<number>::N == expected)
printf("Solution4 passed.\n");
else
printf("Solution4 failed.\n");
}
void Test3()
{
const unsigned int number = 10;
int expected = 55;
Test(number, expected);
if(Sum_Solution4<number>::N == expected)
printf("Solution4 passed.\n");
else
printf("Solution4 failed.\n");
}
void Test4()
{
const unsigned int number = 0;
int expected = 0;
Test(number, expected);
if(Sum_Solution4<number>::N == expected)
printf("Solution4 passed.\n");
else
printf("Solution4 failed.\n");
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
return 0;
}