-
Notifications
You must be signed in to change notification settings - Fork 940
Expand file tree
/
Copy pathmain.cpp
More file actions
46 lines (39 loc) · 1.13 KB
/
main.cpp
File metadata and controls
46 lines (39 loc) · 1.13 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
#include "../exercise.h"
// constexpr unsigned long long fibonacci(int i) {
// switch (i) {
// case 0:
// return 0;
// case 1:
// return 1;
// default:
// return fibonacci(i - 1) + fibonacci(i - 2);
// }
// }
constexpr unsigned long long fibonacci(int i) {
switch (i) {
case 0:
return 0;
case 1:
return 1;
}
unsigned long long fib_1 = 0;
unsigned long long fib_2 = 1;
unsigned long long fib_i = 0;
for (int j = 2; j <= i; ++j) {
fib_i = fib_1 + fib_2;
fib_1 = fib_2;
fib_2 = fib_i;
}
return fib_i;
}
int main(int argc, char **argv) {
constexpr auto FIB20 = fibonacci(20);
ASSERT(FIB20 == 6765, "fibonacci(20) should be 6765");
std::cout << "fibonacci(20) = " << FIB20 << std::endl;
// TODO: 观察错误信息,修改一处,使代码编译运行
// PS: 编译运行,但是不一定能算出结果……
constexpr auto ANS_N = 90;
constexpr auto ANS = fibonacci(ANS_N);
std::cout << "fibonacci(" << ANS_N << ") = " << ANS << std::endl;
return 0;
}