Skip to content

Commit b6cc63d

Browse files
committed
update: 完成 29 题
Signed-off-by: YdrMaster <[email protected]>
1 parent 550fdce commit b6cc63d

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

Diff for: exercises/06_loop/main.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
// TODO: 改正函数实现,实现正确的缓存优化斐波那契计算
44
static unsigned long long fibonacci(int i) {
55
// TODO: 为缓存设置正确的初始值
6-
static unsigned long long cache[128], cached = 2;
6+
static unsigned long long cache[128], cached;
77
// TODO: 设置正确的循环条件
88
for (; false; ++cached) {
99
cache[cached] = cache[cached - 1] + cache[cached - 2];
1010
}
1111
return cache[i];
1212
}
1313

14+
// ---- 不要修改以下代码 ----
1415
int main(int argc, char **argv) {
16+
ASSERT(fibonacci(0) == 0, "fibonacci(0) should be 0");
17+
ASSERT(fibonacci(1) == 1, "fibonacci(1) should be 1");
18+
ASSERT(fibonacci(2) == 1, "fibonacci(2) should be 1");
19+
ASSERT(fibonacci(3) == 2, "fibonacci(3) should be 2");
20+
ASSERT(fibonacci(10) == 55, "fibonacci(10) should be 55");
21+
1522
auto fib100 = fibonacci(100);
16-
ASSERT(fib100 == 3736710778780434371, "fibonacci(100) should be 3736710778780434371");
1723
std::cout << "fibonacci(100) = " << fib100 << std::endl;
24+
ASSERT(fib100 == 3736710778780434371, "fibonacci(100) should be 3736710778780434371");
1825
return 0;
1926
}

Diff for: exercises/15_class_derive/main.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
// 三个类型的定义在下方,它们的关系是:B 派生自 A 并包含一个 X 类型的成员。
66

7+
// ↓↓↓ 这是声明
78
struct X;
89
struct A;
910
struct B;
11+
// ↑↑↑ 这是声明
1012

1113
int main(int argc, char **argv) {
1214
X x = X(1);
@@ -34,6 +36,8 @@ int main(int argc, char **argv) {
3436
return 0;
3537
}
3638

39+
// ↓↓↓ 这是定义
40+
3741
struct X {
3842
int x;
3943

Diff for: exercises/23_std_vector/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ int main(int argc, char **argv) {
88
{
99
std::vector<int> vec{1, 2, 3, 4, 5};
1010
ASSERT(vec.size() == ?, "Fill in the correct value.");
11+
// THINK: `std::vector` 的大小是什么意思?与什么有关?
1112
ASSERT(sizeof(vec) == ?, "Fill in the correct value.");
1213
int ans[]{1, 2, 3, 4, 5};
1314
ASSERT(std::memcmp(vec.?, ans, sizeof(ans)) == 0, "Fill in the correct values.");

Diff for: exercises/27_std_map/main.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
#include "../exercise.h"
22
#include <map>
3-
#include <unordered_map>
43

4+
// READ: `std::map` <https://zh.cppreference.com/w/cpp/container/map>
5+
// READ: `std::unordered_map` <https://zh.cppreference.com/w/cpp/container/unordered_map>
6+
7+
template<class k, class v>
8+
bool key_exists(std::map<k, v> const &map, k const &key) {
9+
// TODO: 实现函数
10+
}
11+
12+
template<class k, class v>
13+
void set(std::map<k, v> &map, k key, v value) {
14+
// TODO: 实现函数
15+
}
16+
17+
// ---- 不要修改以下代码 ----
518
int main(int argc, char **argv) {
6-
ASSERT(false, "todo!");
19+
using namespace std::string_literals;
20+
21+
std::map<std::string, std::string> secrets;
22+
23+
set(secrets, "hello"s, "world"s);
24+
ASSERT(key_exists(secrets, "hello"s), "\"hello\" shoud be in `secrets`");
25+
ASSERT(!key_exists(secrets, "foo"s), "\"foo\" shoud not be in `secrets`");
26+
27+
set(secrets, "foo"s, "bar"s);
28+
set(secrets, "Infini"s, "Tensor"s);
29+
ASSERT(secrets["hello"] == "world", "hello -> world");
30+
ASSERT(secrets["foo"] == "bar", "foo -> bar");
31+
ASSERT(secrets["Infini"] == "Tensor", "Infini -> Tensor");
32+
33+
set(secrets, "hello"s, "developer"s);
34+
ASSERT(secrets["hello"] == "developer", "hello -> developer");
35+
736
return 0;
837
}

Diff for: exercises/28_std_transform/main.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
#include "../exercise.h"
22
#include <algorithm>
3+
#include <string>
4+
#include <vector>
5+
6+
// READ: `std::transform` <https://zh.cppreference.com/w/cpp/algorithm/transform>
7+
// READ: `std::vector::begin` <https://zh.cppreference.com/w/cpp/container/vector/begin>
38

49
int main(int argc, char **argv) {
5-
ASSERT(false, "todo!");
10+
std::vector<int> val{8, 13, 21, 34, 55};
11+
// TODO: 调用 `std::transform`,将 `v` 中的每个元素乘以 2,并转换为字符串,存入 `ans`
12+
// std::vector<std::string> ans
13+
ASSERT(ans.size() == val.size(), "ans size should be equal to val size");
14+
ASSERT(ans[0] == "16", "ans[0] should be 16");
15+
ASSERT(ans[1] == "26", "ans[1] should be 26");
16+
ASSERT(ans[2] == "42", "ans[2] should be 42");
17+
ASSERT(ans[3] == "68", "ans[3] should be 68");
18+
ASSERT(ans[4] == "110", "ans[4] should be 110");
619
return 0;
720
}

Diff for: exercises/29_std_accumulate/main.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#include "../exercise.h"
22
#include <numeric>
33

4+
// READ: `std::accumulate` <https://zh.cppreference.com/w/cpp/algorithm/accumulate>
5+
46
int main(int argc, char **argv) {
5-
ASSERT(false, "todo!");
7+
using DataType = float;
8+
int shape[]{1, 3, 224, 224};
9+
// TODO: 调用 `std::accumulate` 计算 `shape` 的元素之积
10+
// int size =
11+
ASSERT(size = 602112, "4x1x3x224x224 = 602112");
612
return 0;
713
}

0 commit comments

Comments
 (0)