Skip to content

Commit 9d9904f

Browse files
committed
update: 添加题目
Signed-off-by: YdrMaster <[email protected]>
1 parent 98ab1d6 commit 9d9904f

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

exercises/04_static/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.
2-
31
#include <iostream>
42

53
void func(int param) {

exercises/05_constexpr/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// See <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter>.
2-
31
#include <iostream>
42

53
constexpr int fibonacci(int i) {

exercises/06_loop/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
3+
unsigned long long fibonacci(int i) {
4+
static unsigned long long cache[128]{0, 1}, next = 2;
5+
for (; next <= i; ++next) {
6+
cache[next] = cache[next - 1] + cache[next - 2];
7+
}
8+
return cache[i];
9+
}
10+
11+
int main(int argc, char **argv) {
12+
auto fib100 = fibonacci(100);
13+
std::cout << "fibonacci(100) = " << fib100 << std::endl;
14+
return 0;
15+
}

exercises/07_struct/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
struct Fibonacci {
4+
size_t cache[128];
5+
int next = 2;
6+
7+
Fibonacci() : cache{0, 1}, next(2) {}
8+
~Fibonacci() {
9+
std::cout << "Drop Fibonacci with next = " << next << std::endl;
10+
}
11+
12+
size_t operator[](int i) {
13+
for (; next <= i; ++next) {
14+
cache[next] = cache[next - 1] + cache[next - 2];
15+
}
16+
return cache[i];
17+
}
18+
};
19+
20+
int main(int argc, char **argv) {
21+
Fibonacci fib0, fib1;
22+
std::cout << "fibonacci(10) = " << fib0[10] << std::endl;
23+
std::cout << "fibonacci(100) = " << fib1[100] << std::endl;
24+
return 0;
25+
}

exercises/08_class/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
3+
class Fibonacci {
4+
size_t cache[128];
5+
int next = 2;
6+
7+
public:
8+
Fibonacci() : cache{0, 1}, next(2) {}
9+
~Fibonacci() {
10+
std::cout << "Drop Fibonacci with next = " << next << std::endl;
11+
}
12+
13+
size_t operator[](int i) {
14+
for (; next <= i; ++next) {
15+
cache[next] = cache[next - 1] + cache[next - 2];
16+
}
17+
return cache[i];
18+
}
19+
};
20+
21+
int main(int argc, char **argv) {
22+
Fibonacci fib0, fib1;
23+
std::cout << "fibonacci(10) = " << fib0[10] << std::endl;
24+
std::cout << "fibonacci(100) = " << fib1[100] << std::endl;
25+
return 0;
26+
}

xmake.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ target("exercise04")
2424
target("exercise05")
2525
set_kind("binary")
2626
add_files("exercises/05_constexpr/main.cpp")
27+
28+
target("exercise06")
29+
set_kind("binary")
30+
add_files("exercises/06_loop/main.cpp")
31+
32+
target("exercise07")
33+
set_kind("binary")
34+
add_files("exercises/07_struct/main.cpp")
35+
36+
target("exercise08")
37+
set_kind("binary")
38+
add_files("exercises/08_class/main.cpp")

0 commit comments

Comments
 (0)