diff --git a/exercises/06_loop/main.cpp b/exercises/06_loop/main.cpp index 0b01455a..3c04483c 100644 --- a/exercises/06_loop/main.cpp +++ b/exercises/06_loop/main.cpp @@ -3,7 +3,7 @@ // TODO: 改正函数实现,实现正确的缓存优化斐波那契计算 static unsigned long long fibonacci(int i) { // TODO: 为缓存设置正确的初始值 - static unsigned long long cache[128], cached = 2; + static unsigned long long cache[128], cached; // TODO: 设置正确的循环条件 for (; false; ++cached) { cache[cached] = cache[cached - 1] + cache[cached - 2]; @@ -11,9 +11,16 @@ static unsigned long long fibonacci(int i) { return cache[i]; } +// ---- 不要修改以下代码 ---- int main(int argc, char **argv) { + ASSERT(fibonacci(0) == 0, "fibonacci(0) should be 0"); + ASSERT(fibonacci(1) == 1, "fibonacci(1) should be 1"); + ASSERT(fibonacci(2) == 1, "fibonacci(2) should be 1"); + ASSERT(fibonacci(3) == 2, "fibonacci(3) should be 2"); + ASSERT(fibonacci(10) == 55, "fibonacci(10) should be 55"); + auto fib100 = fibonacci(100); - ASSERT(fib100 == 3736710778780434371, "fibonacci(100) should be 3736710778780434371"); std::cout << "fibonacci(100) = " << fib100 << std::endl; + ASSERT(fib100 == 3736710778780434371, "fibonacci(100) should be 3736710778780434371"); return 0; } diff --git a/exercises/15_class_derive/main.cpp b/exercises/15_class_derive/main.cpp index ea653a01..fa3faec0 100644 --- a/exercises/15_class_derive/main.cpp +++ b/exercises/15_class_derive/main.cpp @@ -4,9 +4,11 @@ // 三个类型的定义在下方,它们的关系是:B 派生自 A 并包含一个 X 类型的成员。 +// ↓↓↓ 这是声明 struct X; struct A; struct B; +// ↑↑↑ 这是声明 int main(int argc, char **argv) { X x = X(1); @@ -34,6 +36,8 @@ int main(int argc, char **argv) { return 0; } +// ↓↓↓ 这是定义 + struct X { int x; diff --git a/exercises/23_std_vector/main.cpp b/exercises/23_std_vector/main.cpp index c6b48172..8358a9d1 100644 --- a/exercises/23_std_vector/main.cpp +++ b/exercises/23_std_vector/main.cpp @@ -8,6 +8,7 @@ int main(int argc, char **argv) { { std::vector vec{1, 2, 3, 4, 5}; ASSERT(vec.size() == ?, "Fill in the correct value."); + // THINK: `std::vector` 的大小是什么意思?与什么有关? ASSERT(sizeof(vec) == ?, "Fill in the correct value."); int ans[]{1, 2, 3, 4, 5}; ASSERT(std::memcmp(vec.?, ans, sizeof(ans)) == 0, "Fill in the correct values."); diff --git a/exercises/27_std_map/main.cpp b/exercises/27_std_map/main.cpp index 98a2f0b0..fcccca34 100644 --- a/exercises/27_std_map/main.cpp +++ b/exercises/27_std_map/main.cpp @@ -1,8 +1,37 @@ #include "../exercise.h" #include -#include +// READ: `std::map` +// READ: `std::unordered_map` + +template +bool key_exists(std::map const &map, k const &key) { + // TODO: 实现函数 +} + +template +void set(std::map &map, k key, v value) { + // TODO: 实现函数 +} + +// ---- 不要修改以下代码 ---- int main(int argc, char **argv) { - ASSERT(false, "todo!"); + using namespace std::string_literals; + + std::map secrets; + + set(secrets, "hello"s, "world"s); + ASSERT(key_exists(secrets, "hello"s), "\"hello\" shoud be in `secrets`"); + ASSERT(!key_exists(secrets, "foo"s), "\"foo\" shoud not be in `secrets`"); + + set(secrets, "foo"s, "bar"s); + set(secrets, "Infini"s, "Tensor"s); + ASSERT(secrets["hello"] == "world", "hello -> world"); + ASSERT(secrets["foo"] == "bar", "foo -> bar"); + ASSERT(secrets["Infini"] == "Tensor", "Infini -> Tensor"); + + set(secrets, "hello"s, "developer"s); + ASSERT(secrets["hello"] == "developer", "hello -> developer"); + return 0; } diff --git a/exercises/28_std_transform/main.cpp b/exercises/28_std_transform/main.cpp index a94bcedb..f4dc25a5 100644 --- a/exercises/28_std_transform/main.cpp +++ b/exercises/28_std_transform/main.cpp @@ -1,7 +1,20 @@ #include "../exercise.h" #include +#include +#include + +// READ: `std::transform` +// READ: `std::vector::begin` int main(int argc, char **argv) { - ASSERT(false, "todo!"); + std::vector val{8, 13, 21, 34, 55}; + // TODO: 调用 `std::transform`,将 `v` 中的每个元素乘以 2,并转换为字符串,存入 `ans` + // std::vector ans + ASSERT(ans.size() == val.size(), "ans size should be equal to val size"); + ASSERT(ans[0] == "16", "ans[0] should be 16"); + ASSERT(ans[1] == "26", "ans[1] should be 26"); + ASSERT(ans[2] == "42", "ans[2] should be 42"); + ASSERT(ans[3] == "68", "ans[3] should be 68"); + ASSERT(ans[4] == "110", "ans[4] should be 110"); return 0; } diff --git a/exercises/29_std_accumulate/main.cpp b/exercises/29_std_accumulate/main.cpp index ac240036..7d9a8da7 100644 --- a/exercises/29_std_accumulate/main.cpp +++ b/exercises/29_std_accumulate/main.cpp @@ -1,7 +1,13 @@ #include "../exercise.h" #include +// READ: `std::accumulate` + int main(int argc, char **argv) { - ASSERT(false, "todo!"); + using DataType = float; + int shape[]{1, 3, 224, 224}; + // TODO: 调用 `std::accumulate` 计算 `shape` 的元素之积 + // int size = + ASSERT(size = 602112, "4x1x3x224x224 = 602112"); return 0; }