Skip to content

Commit

Permalink
update: 完成 29 题
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 17, 2024
1 parent 550fdce commit b6cc63d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
11 changes: 9 additions & 2 deletions exercises/06_loop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
// 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];
}
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;
}
4 changes: 4 additions & 0 deletions exercises/15_class_derive/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

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

// ↓↓↓ 这是声明
struct X;
struct A;
struct B;
// ↑↑↑ 这是声明

int main(int argc, char **argv) {
X x = X(1);
Expand Down Expand Up @@ -34,6 +36,8 @@ int main(int argc, char **argv) {
return 0;
}

// ↓↓↓ 这是定义

struct X {
int x;

Expand Down
1 change: 1 addition & 0 deletions exercises/23_std_vector/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ int main(int argc, char **argv) {
{
std::vector<int> 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.");
Expand Down
33 changes: 31 additions & 2 deletions exercises/27_std_map/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
#include "../exercise.h"
#include <map>
#include <unordered_map>

// READ: `std::map` <https://zh.cppreference.com/w/cpp/container/map>
// READ: `std::unordered_map` <https://zh.cppreference.com/w/cpp/container/unordered_map>

template<class k, class v>
bool key_exists(std::map<k, v> const &map, k const &key) {
// TODO: 实现函数
}

template<class k, class v>
void set(std::map<k, v> &map, k key, v value) {
// TODO: 实现函数
}

// ---- 不要修改以下代码 ----
int main(int argc, char **argv) {
ASSERT(false, "todo!");
using namespace std::string_literals;

std::map<std::string, std::string> 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;
}
15 changes: 14 additions & 1 deletion exercises/28_std_transform/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#include "../exercise.h"
#include <algorithm>
#include <string>
#include <vector>

// READ: `std::transform` <https://zh.cppreference.com/w/cpp/algorithm/transform>
// READ: `std::vector::begin` <https://zh.cppreference.com/w/cpp/container/vector/begin>

int main(int argc, char **argv) {
ASSERT(false, "todo!");
std::vector<int> val{8, 13, 21, 34, 55};
// TODO: 调用 `std::transform`,将 `v` 中的每个元素乘以 2,并转换为字符串,存入 `ans`
// std::vector<std::string> 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;
}
8 changes: 7 additions & 1 deletion exercises/29_std_accumulate/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include "../exercise.h"
#include <numeric>

// READ: `std::accumulate` <https://zh.cppreference.com/w/cpp/algorithm/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;
}

0 comments on commit b6cc63d

Please sign in to comment.