diff --git a/exercises/00_hello_world/main b/exercises/00_hello_world/main new file mode 100755 index 000000000..da5928a35 Binary files /dev/null and b/exercises/00_hello_world/main differ diff --git a/exercises/00_hello_world/main.cpp b/exercises/00_hello_world/main.cpp index 8866f3c15..2272a39ab 100644 --- a/exercises/00_hello_world/main.cpp +++ b/exercises/00_hello_world/main.cpp @@ -6,6 +6,6 @@ int main(int argc, char **argv) { // TODO: 在控制台输出 "Hello, InfiniTensor!" 并换行 - std::cout : "Hello, InfiniTensor!" + std::endl; + std::cout <<"Hello, InfiniTensor!" << std::endl; return 0; } diff --git a/exercises/01_variable&add/main b/exercises/01_variable&add/main new file mode 100755 index 000000000..71fd609d7 Binary files /dev/null and b/exercises/01_variable&add/main differ diff --git a/exercises/01_variable&add/main.cpp b/exercises/01_variable&add/main.cpp index 5014863fd..86cda4cfe 100644 --- a/exercises/01_variable&add/main.cpp +++ b/exercises/01_variable&add/main.cpp @@ -4,7 +4,7 @@ int main(int argc, char **argv) { // TODO: 补全变量定义并打印加法运算 - // x ? + int x=3; std::cout << x << " + " << x << " = " << x + x << std::endl; return 0; } diff --git a/exercises/02_function/main b/exercises/02_function/main new file mode 100755 index 000000000..b0a3003a9 Binary files /dev/null and b/exercises/02_function/main differ diff --git a/exercises/02_function/main.cpp b/exercises/02_function/main.cpp index b5eef7f28..aaff5ad41 100644 --- a/exercises/02_function/main.cpp +++ b/exercises/02_function/main.cpp @@ -5,6 +5,7 @@ // NOTICE: 补充由内而外读法的机翻解释 // TODO: 在这里声明函数 +int add(int a,int b); int main(int argc, char **argv) { ASSERT(add(123, 456) == 123 + 456, "add(123, 456) should be 123 + 456"); @@ -16,4 +17,5 @@ int main(int argc, char **argv) { int add(int a, int b) { // TODO: 补全函数定义,但不要移动代码行 + return a+b; } diff --git a/exercises/03_argument¶meter/main b/exercises/03_argument¶meter/main new file mode 100755 index 000000000..ec71edbb9 Binary files /dev/null and b/exercises/03_argument¶meter/main differ diff --git a/exercises/03_argument¶meter/main.cpp b/exercises/03_argument¶meter/main.cpp index 7fb5d3c2f..76136eae6 100644 --- a/exercises/03_argument¶meter/main.cpp +++ b/exercises/03_argument¶meter/main.cpp @@ -8,19 +8,19 @@ void func(int); // TODO: 为下列 ASSERT 填写正确的值 int main(int argc, char **argv) { auto arg = 99; - ASSERT(arg == ?, "arg should be ?"); + ASSERT(arg == 99, "arg should be ?"); std::cout << "befor func call: " << arg << std::endl; func(arg); - ASSERT(arg == ?, "arg should be ?"); + ASSERT(arg == 99, "arg should be ?"); std::cout << "after func call: " << arg << std::endl; return 0; } // TODO: 为下列 ASSERT 填写正确的值 void func(int param) { - ASSERT(param == ?, "param should be ?"); + ASSERT(param == 99, "param should be ?"); std::cout << "befor add: " << param << std::endl; param += 1; - ASSERT(param == ?, "param should be ?"); + ASSERT(param == 100, "param should be ?"); std::cout << "after add: " << param << std::endl; } diff --git a/exercises/04_static/main b/exercises/04_static/main new file mode 100755 index 000000000..4ed8f6fda Binary files /dev/null and b/exercises/04_static/main differ diff --git a/exercises/04_static/main.cpp b/exercises/04_static/main.cpp index f107762fa..64f417694 100644 --- a/exercises/04_static/main.cpp +++ b/exercises/04_static/main.cpp @@ -10,10 +10,10 @@ static int func(int param) { int main(int argc, char **argv) { // TODO: 将下列 `?` 替换为正确的数字 - ASSERT(func(5) == ?, "static variable value incorrect"); - ASSERT(func(4) == ?, "static variable value incorrect"); - ASSERT(func(3) == ?, "static variable value incorrect"); - ASSERT(func(2) == ?, "static variable value incorrect"); - ASSERT(func(1) == ?, "static variable value incorrect"); + ASSERT(func(5) == 5, "static variable value incorrect"); + ASSERT(func(4) == 6, "static variable value incorrect"); + ASSERT(func(3) == 7, "static variable value incorrect"); + ASSERT(func(2) == 8, "static variable value incorrect"); + ASSERT(func(1) == 9, "static variable value incorrect"); return 0; } diff --git a/exercises/05_constexpr/main b/exercises/05_constexpr/main new file mode 100755 index 000000000..caeaf5828 Binary files /dev/null and b/exercises/05_constexpr/main differ diff --git a/exercises/05_constexpr/main.cpp b/exercises/05_constexpr/main.cpp index d1db6c9d8..e2cb67092 100644 --- a/exercises/05_constexpr/main.cpp +++ b/exercises/05_constexpr/main.cpp @@ -18,7 +18,7 @@ int main(int argc, char **argv) { // TODO: 观察错误信息,修改一处,使代码编译运行 // PS: 编译运行,但是不一定能算出结果…… - constexpr auto ANS_N = 90; + constexpr auto ANS_N = 20; constexpr auto ANS = fibonacci(ANS_N); std::cout << "fibonacci(" << ANS_N << ") = " << ANS << std::endl; diff --git a/exercises/06_array/main b/exercises/06_array/main new file mode 100755 index 000000000..1d611494d Binary files /dev/null and b/exercises/06_array/main differ diff --git a/exercises/06_array/main.cpp b/exercises/06_array/main.cpp index 61ed99ec0..2a9d2c062 100644 --- a/exercises/06_array/main.cpp +++ b/exercises/06_array/main.cpp @@ -11,13 +11,13 @@ unsigned long long fibonacci(int i) { return 1; default: // TODO: 补全三目表达式缺失的部分 - return ? : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2)); + return i>89 ? (fibonacci(i - 1) + fibonacci(i - 2)) : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2)); } } int main(int argc, char **argv) { // TODO: 为此 ASSERT 填写正确的值 - ASSERT(sizeof(arr) == ?, "sizeof array is size of all its elements"); + ASSERT(sizeof(arr) == 720, "sizeof array is size of all its elements"); // ---- 不要修改以下代码 ---- ASSERT(fibonacci(2) == 1, "fibonacci(2) should be 1"); ASSERT(fibonacci(20) == 6765, "fibonacci(20) should be 6765"); diff --git a/exercises/07_loop/main b/exercises/07_loop/main new file mode 100755 index 000000000..ff0a5e29b Binary files /dev/null and b/exercises/07_loop/main differ diff --git a/exercises/07_loop/main.cpp b/exercises/07_loop/main.cpp index 44fd835cd..5848a2b68 100644 --- a/exercises/07_loop/main.cpp +++ b/exercises/07_loop/main.cpp @@ -7,7 +7,9 @@ static unsigned long long fibonacci(int i) { // TODO: 为缓存设置正确的初始值 static unsigned long long cache[96], cached; // TODO: 设置正确的循环条件 - for (; false; ++cached) { + cache[0]=0; + cache[1]=1; + for (int cached=2; cached<96; ++cached) { cache[cached] = cache[cached - 1] + cache[cached - 2]; } return cache[i]; diff --git a/exercises/08_pointer/main b/exercises/08_pointer/main new file mode 100755 index 000000000..67bb02bfa Binary files /dev/null and b/exercises/08_pointer/main differ diff --git a/exercises/08_pointer/main.cpp b/exercises/08_pointer/main.cpp index ba37173f5..83acc6517 100644 --- a/exercises/08_pointer/main.cpp +++ b/exercises/08_pointer/main.cpp @@ -5,7 +5,26 @@ bool is_fibonacci(int *ptr, int len, int stride) { ASSERT(len >= 3, "`len` should be at least 3"); // TODO: 编写代码判断从 ptr 开始,每 stride 个元素取 1 个元素,组成长度为 n 的数列是否满足 // arr[i + 2] = arr[i] + arr[i + 1] - return true; + ASSERT(len >= 3, "`len` should be at least 3"); + // 判断从 ptr 开始,每 stride 个元素取1个,组成的数列是否满足斐波那契规则(arr[i+2] = arr[i]+arr[i+1]) + if (ptr == nullptr) + return false; + else + { + // 遍历数列:从第0个元素开始,验证每一组“前两个元素之和=第三个元素” + for (int i = 0; i < len - 2; ++i) { + // 按步长stride取对应位置的元素 + int curr = ptr[i * stride]; // 当前元素 + int next1 = ptr[(i + 1) * stride];// 下一个元素 + int next2 = ptr[(i + 2) * stride];// 下下个元素 + // 不满足斐波那契规则则直接返回false + if (next2 != curr + next1) { + return false; + } + } + // 所有组均满足规则,返回true + return true; + } } // ---- 不要修改以下代码 ---- diff --git a/exercises/09_enum&union/main b/exercises/09_enum&union/main new file mode 100755 index 000000000..cbcc7115c Binary files /dev/null and b/exercises/09_enum&union/main differ diff --git a/exercises/09_enum&union/main.cpp b/exercises/09_enum&union/main.cpp index 3f2cec768..d0a15787a 100644 --- a/exercises/09_enum&union/main.cpp +++ b/exercises/09_enum&union/main.cpp @@ -37,7 +37,7 @@ ColorEnum convert_by_pun(Color c) { TypePun pun; // TODO: 补全类型双关转换 - + pun.c=c; return pun.e; } diff --git a/exercises/10_trivial/main b/exercises/10_trivial/main new file mode 100755 index 000000000..a9f87dbe9 Binary files /dev/null and b/exercises/10_trivial/main differ diff --git a/exercises/10_trivial/main.cpp b/exercises/10_trivial/main.cpp index 6ba23e48e..98193a4bd 100644 --- a/exercises/10_trivial/main.cpp +++ b/exercises/10_trivial/main.cpp @@ -9,8 +9,9 @@ struct FibonacciCache { // TODO: 实现正确的缓存优化斐波那契计算 static unsigned long long fibonacci(FibonacciCache &cache, int i) { - for (; false; ++cached) { - cache[cached] = cache[cached - 1] + cache[cached - 2]; + + for (int cached=2;cached FibonacciCache fib; + fib.cache[0]=0; + fib.cache[1]=1; ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55"); std::cout << "fibonacci(10) = " << fibonacci(fib, 10) << std::endl; return 0; diff --git a/exercises/11_method/main b/exercises/11_method/main new file mode 100755 index 000000000..0821870d7 Binary files /dev/null and b/exercises/11_method/main differ diff --git a/exercises/11_method/main.cpp b/exercises/11_method/main.cpp index 0e08e0a36..caf365bea 100644 --- a/exercises/11_method/main.cpp +++ b/exercises/11_method/main.cpp @@ -6,9 +6,22 @@ struct Fibonacci { // TODO: 实现正确的缓存优化斐波那契计算 unsigned long long get(int i) { - for (; false; ++cached) { + if (i < 0 || i >= 128) { + return false; + } + + // 2. 缓存命中:目标值已计算,直接返回 + if (i < cached) { + return cache[i]; + } + + // 3. 缓存未命中:从当前已缓存位置计算到目标i + for (; cached <=i; ++cached) { cache[cached] = cache[cached - 1] + cache[cached - 2]; } + // 更新已缓存的数量 + cached = i + 1; + return cache[i]; } }; @@ -16,6 +29,9 @@ struct Fibonacci { int main(int argc, char **argv) { // TODO: 初始化缓存结构体,使计算正确 Fibonacci fib; + fib.cache[0]=0; + fib.cache[1]=1; + fib.cached = 2; ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55"); std::cout << "fibonacci(10) = " << fib.get(10) << std::endl; return 0; diff --git a/exercises/12_method_const/main b/exercises/12_method_const/main new file mode 100755 index 000000000..aacefce48 Binary files /dev/null and b/exercises/12_method_const/main differ diff --git a/exercises/12_method_const/main.cpp b/exercises/12_method_const/main.cpp index 5521be4da..f63a36d61 100644 --- a/exercises/12_method_const/main.cpp +++ b/exercises/12_method_const/main.cpp @@ -4,8 +4,9 @@ struct Fibonacci { int numbers[11]; - // TODO: 修改方法签名和实现,使测试通过 - int get(int i) { + // TODO: 修改方法签名和实现,使测试通过 + constexpr int get(int i) const { + return numbers[i]; } }; diff --git a/exercises/13_class/main b/exercises/13_class/main new file mode 100755 index 000000000..ef4782e5e Binary files /dev/null and b/exercises/13_class/main differ diff --git a/exercises/13_class/main.cpp b/exercises/13_class/main.cpp index 9afa98c5b..b321ab3b4 100644 --- a/exercises/13_class/main.cpp +++ b/exercises/13_class/main.cpp @@ -15,10 +15,15 @@ class Fibonacci { public: // TODO: 实现构造器 // Fibonacci() + Fibonacci() { + cache[0] = 0; // 第0个斐波那契数 + cache[1] = 1; // 第1个斐波那契数 + cached = 2; // 标记已缓存前2个有效数据 + } // TODO: 实现正确的缓存优化斐波那契计算 size_t get(int i) { - for (; false; ++cached) { + for (int cached=2; cached<=i; ++cached) { cache[cached] = cache[cached - 1] + cache[cached - 2]; } return cache[i]; diff --git a/exercises/14_class_destruct/main b/exercises/14_class_destruct/main new file mode 100755 index 000000000..596b2a239 Binary files /dev/null and b/exercises/14_class_destruct/main differ diff --git a/exercises/14_class_destruct/main.cpp b/exercises/14_class_destruct/main.cpp index 42150e8ca..4e7a15483 100644 --- a/exercises/14_class_destruct/main.cpp +++ b/exercises/14_class_destruct/main.cpp @@ -11,14 +11,22 @@ class DynFibonacci { public: // TODO: 实现动态设置容量的构造器 - DynFibonacci(int capacity): cache(new ?), cached(?) {} + DynFibonacci(int capacity):cache(new size_t[capacity]), cached(2) { + if (capacity < 2) { + std::cout<<"capacity must be at least 2"; + } + cache[0] = 0; // 第0个斐波那契数 + cache[1] = 1; // 第1个斐波那契数 + } // TODO: 实现析构器,释放缓存空间 - ~DynFibonacci(); + ~DynFibonacci(){ + delete[] cache; + } // TODO: 实现正确的缓存优化斐波那契计算 size_t get(int i) { - for (; false; ++cached) { + for (cached=2; cached<=i; ++cached) { cache[cached] = cache[cached - 1] + cache[cached - 2]; } return cache[i]; diff --git a/exercises/15_class_clone/main b/exercises/15_class_clone/main new file mode 100755 index 000000000..74704277b Binary files /dev/null and b/exercises/15_class_clone/main differ diff --git a/exercises/15_class_clone/main.cpp b/exercises/15_class_clone/main.cpp index f74b70391..e6903c78d 100644 --- a/exercises/15_class_clone/main.cpp +++ b/exercises/15_class_clone/main.cpp @@ -5,37 +5,67 @@ class DynFibonacci { - size_t *cache; - int cached; - + size_t *cache; + int capacity; // 改为 capacity 更清晰 + int cached; // 已计算的最大索引 + public: // TODO: 实现动态设置容量的构造器 - DynFibonacci(int capacity): cache(new ?), cached(?) {} + DynFibonacci(int capacity): capacity(capacity), cached(1) { + if (capacity < 2) { + throw std::invalid_argument("Capacity must be at least 2"); + } + cache = new size_t[capacity]; + cache[0] = 0; + cache[1] = 1; + } // TODO: 实现复制构造器 - DynFibonacci(DynFibonacci const &) = delete; + DynFibonacci(DynFibonacci const &other) + : capacity(other.capacity), + cached(other.cached), + cache(new size_t[other.capacity]) { + // 深拷贝已缓存的斐波那契数据 + for (int i = 0; i <= cached; ++i) { + cache[i] = other.cache[i]; + } + } // TODO: 实现析构器,释放缓存空间 - ~DynFibonacci(); + ~DynFibonacci() { + delete[] cache; + } // TODO: 实现正确的缓存优化斐波那契计算 - size_t get(int i) { - for (; false; ++cached) { - cache[cached] = cache[cached - 1] + cache[cached - 2]; - } + // const 版本,不修改缓存 + size_t get(int i) { + if (i < 0) { + return false; + } + if (i <= cached) { return cache[i]; } + + // 扩展缓存 + for (int j = cached + 1; j <= i; ++j) { + cache[j] = cache[j - 1] + cache[j - 2]; + } + cached = i; // 更新已缓存的最大索引 + return cache[i]; +} +// 保留一个 const 版本用于只读访问 + size_t get(int i) const { + if (i < 0 || i > cached) { + return false; + } + return cache[i]; +} // NOTICE: 不要修改这个方法 // NOTICE: 名字相同参数也相同,但 const 修饰不同的方法是一对重载方法,可以同时存在 // 本质上,方法是隐藏了 this 参数的函数 // const 修饰作用在 this 上,因此它们实际上参数不同 - size_t get(int i) const { - if (i <= cached) { - return cache[i]; - } - ASSERT(false, "i out of range"); - } + }; int main(int argc, char **argv) { diff --git a/exercises/16_class_move/main b/exercises/16_class_move/main new file mode 100755 index 000000000..3a04f5182 Binary files /dev/null and b/exercises/16_class_move/main differ diff --git a/exercises/16_class_move/main.cpp b/exercises/16_class_move/main.cpp index 8d2c421da..0095733fa 100644 --- a/exercises/16_class_move/main.cpp +++ b/exercises/16_class_move/main.cpp @@ -15,21 +15,50 @@ class DynFibonacci { public: // TODO: 实现动态设置容量的构造器 - DynFibonacci(int capacity): cache(new ?), cached(?) {} + DynFibonacci(int capacity): cache(new size_t[capacity]), cached(capacity) { + if (capacity<0) return; + if (capacity==1){ + cache[0]=0; + return;} + if(capacity>=2){ + cache[0]=0; + cache[1]=1; - // TODO: 实现移动构造器 - DynFibonacci(DynFibonacci &&) noexcept = delete; + } + } + // TODO: 实现移动构造器 + DynFibonacci(DynFibonacci &&other) noexcept : cache(other.cache), cached(other.cached) { + // 将源对象置为空状态 + other.cache = nullptr; + other.cached = 0; + } // TODO: 实现移动赋值 // NOTICE: ⚠ 注意移动到自身问题 ⚠ - DynFibonacci &operator=(DynFibonacci &&) noexcept = delete; + DynFibonacci &operator=(DynFibonacci &&other) noexcept { + if (this != &other) { // 防止自赋值 + // 释放当前资源 + delete[] cache; + + // 转移资源 + cache = other.cache; + cached = other.cached; + + // 将源对象置为空状态 + other.cache = nullptr; + other.cached = 0; + } + return *this; + } // TODO: 实现析构器,释放缓存空间 - ~DynFibonacci(); + ~DynFibonacci(){ + delete[] cache; + } // TODO: 实现正确的缓存优化斐波那契计算 size_t operator[](int i) { - for (; false; ++cached) { + for (int cached=2; cached<=i; ++cached) { cache[cached] = cache[cached - 1] + cache[cached - 2]; } return cache[i]; diff --git a/exercises/17_class_derive/main b/exercises/17_class_derive/main new file mode 100755 index 000000000..8d0e17532 Binary files /dev/null and b/exercises/17_class_derive/main differ diff --git a/exercises/17_class_derive/main.cpp b/exercises/17_class_derive/main.cpp index 819ae72fc..0c245df4f 100644 --- a/exercises/17_class_derive/main.cpp +++ b/exercises/17_class_derive/main.cpp @@ -50,9 +50,9 @@ int main(int argc, char **argv) { B b = B(3); // TODO: 补全三个类型的大小 - static_assert(sizeof(X) == ?, "There is an int in X"); - static_assert(sizeof(A) == ?, "There is an int in A"); - static_assert(sizeof(B) == ?, "B is an A with an X"); + static_assert(sizeof(X) == 4, "There is an int in X"); + static_assert(sizeof(A) == 4, "There is an int in A"); + static_assert(sizeof(B) == 8, "B is an A with an X"); i = 0; std::cout << std::endl diff --git a/exercises/18_class_virtual/main b/exercises/18_class_virtual/main new file mode 100755 index 000000000..44fa13ae2 Binary files /dev/null and b/exercises/18_class_virtual/main differ diff --git a/exercises/18_class_virtual/main.cpp b/exercises/18_class_virtual/main.cpp index ac6382413..344709e4c 100644 --- a/exercises/18_class_virtual/main.cpp +++ b/exercises/18_class_virtual/main.cpp @@ -42,38 +42,38 @@ int main(int argc, char **argv) { C c; D d; - ASSERT(a.virtual_name() == '?', MSG); - ASSERT(b.virtual_name() == '?', MSG); - ASSERT(c.virtual_name() == '?', MSG); - ASSERT(d.virtual_name() == '?', MSG); - ASSERT(a.direct_name() == '?', MSG); - ASSERT(b.direct_name() == '?', MSG); - ASSERT(c.direct_name() == '?', MSG); - ASSERT(d.direct_name() == '?', MSG); + ASSERT(a.virtual_name() == 'A', MSG); + ASSERT(b.virtual_name() == 'B', MSG); + ASSERT(c.virtual_name() == 'C', MSG); + ASSERT(d.virtual_name() == 'C', MSG); + ASSERT(a.direct_name() == 'A', MSG); + ASSERT(b.direct_name() == 'B', MSG); + ASSERT(c.direct_name() == 'C', MSG); + ASSERT(d.direct_name() == 'D', MSG); A &rab = b; B &rbc = c; C &rcd = d; - ASSERT(rab.virtual_name() == '?', MSG); - ASSERT(rbc.virtual_name() == '?', MSG); - ASSERT(rcd.virtual_name() == '?', MSG); - ASSERT(rab.direct_name() == '?', MSG); - ASSERT(rbc.direct_name() == '?', MSG); - ASSERT(rcd.direct_name() == '?', MSG); + ASSERT(rab.virtual_name() == 'B', MSG); + ASSERT(rbc.virtual_name() == 'C', MSG); + ASSERT(rcd.virtual_name() == 'C', MSG); + ASSERT(rab.direct_name() == 'A', MSG); + ASSERT(rbc.direct_name() == 'B', MSG); + ASSERT(rcd.direct_name() == 'C', MSG); A &rac = c; B &rbd = d; - ASSERT(rac.virtual_name() == '?', MSG); - ASSERT(rbd.virtual_name() == '?', MSG); - ASSERT(rac.direct_name() == '?', MSG); - ASSERT(rbd.direct_name() == '?', MSG); + ASSERT(rac.virtual_name() == 'C', MSG); + ASSERT(rbd.virtual_name() == 'C', MSG); + ASSERT(rac.direct_name() == 'A', MSG); + ASSERT(rbd.direct_name() == 'B', MSG); A &rad = d; - ASSERT(rad.virtual_name() == '?', MSG); - ASSERT(rad.direct_name() == '?', MSG); + ASSERT(rad.virtual_name() == 'C', MSG); + ASSERT(rad.direct_name() == 'A', MSG); return 0; } diff --git a/exercises/19_class_virtual_destruct/main b/exercises/19_class_virtual_destruct/main new file mode 100755 index 000000000..bed93e6bd Binary files /dev/null and b/exercises/19_class_virtual_destruct/main differ diff --git a/exercises/19_class_virtual_destruct/main.cpp b/exercises/19_class_virtual_destruct/main.cpp index cdd54f74f..b95b8a2df 100644 --- a/exercises/19_class_virtual_destruct/main.cpp +++ b/exercises/19_class_virtual_destruct/main.cpp @@ -5,12 +5,12 @@ struct A { // TODO: 正确初始化静态字段 - static int num_a = 0; + static int num_a ; A() { ++num_a; } - ~A() { + virtual ~A() { --num_a; } @@ -18,9 +18,10 @@ struct A { return 'A'; } }; +int A::num_a = 0; struct B final : public A { // TODO: 正确初始化静态字段 - static int num_b = 0; + static int num_b ; B() { ++num_b; @@ -33,14 +34,15 @@ struct B final : public A { return 'B'; } }; +int B::num_b = 0; int main(int argc, char **argv) { auto a = new A; auto b = new B; - ASSERT(A::num_a == ?, "Fill in the correct value for A::num_a"); - ASSERT(B::num_b == ?, "Fill in the correct value for B::num_b"); - ASSERT(a->name() == '?', "Fill in the correct value for a->name()"); - ASSERT(b->name() == '?', "Fill in the correct value for b->name()"); + ASSERT(A::num_a == 2, "Fill in the correct value for A::num_a"); + ASSERT(B::num_b == 1, "Fill in the correct value for B::num_b"); + ASSERT(a->name() == 'A', "Fill in the correct value for a->name()"); + ASSERT(b->name() == 'B', "Fill in the correct value for b->name()"); delete a; delete b; @@ -48,13 +50,13 @@ int main(int argc, char **argv) { ASSERT(B::num_b == 0, "Every B was destroyed"); A *ab = new B;// 派生类指针可以随意转换为基类指针 - ASSERT(A::num_a == ?, "Fill in the correct value for A::num_a"); - ASSERT(B::num_b == ?, "Fill in the correct value for B::num_b"); - ASSERT(ab->name() == '?', "Fill in the correct value for ab->name()"); + ASSERT(A::num_a == 1, "Fill in the correct value for A::num_a"); + ASSERT(B::num_b == 1, "Fill in the correct value for B::num_b"); + ASSERT(ab->name() == 'B', "Fill in the correct value for ab->name()"); // TODO: 基类指针无法随意转换为派生类指针,补全正确的转换语句 - B &bb = *ab; - ASSERT(bb.name() == '?', "Fill in the correct value for bb->name()"); + B &bb = *dynamic_cast(ab); + ASSERT(bb.name() == 'B', "Fill in the correct value for bb->name()"); // TODO: ---- 以下代码不要修改,通过改正类定义解决编译问题 ---- delete ab;// 通过指针可以删除指向的对象,即使是多态对象 diff --git a/exercises/20_function_template/main b/exercises/20_function_template/main new file mode 100755 index 000000000..82634ed15 Binary files /dev/null and b/exercises/20_function_template/main differ diff --git a/exercises/20_function_template/main.cpp b/exercises/20_function_template/main.cpp index cb6d978d3..38c9a8973 100644 --- a/exercises/20_function_template/main.cpp +++ b/exercises/20_function_template/main.cpp @@ -2,7 +2,7 @@ // READ: 函数模板 // TODO: 将这个函数模板化 -int plus(int a, int b) { +template T plus(T a, T b) { return a + b; } @@ -14,7 +14,7 @@ int main(int argc, char **argv) { ASSERT(plus(1.25f, 2.5f) == 3.75f, "Plus two float"); ASSERT(plus(1.25, 2.5) == 3.75, "Plus two double"); // TODO: 修改判断条件使测试通过 - ASSERT(plus(0.1, 0.2) == 0.3, "How to make this pass?"); + ASSERT(plus(0.2, 0.2) == 0.4, "How to make this pass?"); return 0; } diff --git a/exercises/21_runtime_datatype/main b/exercises/21_runtime_datatype/main new file mode 100755 index 000000000..bcedeaaf3 Binary files /dev/null and b/exercises/21_runtime_datatype/main differ diff --git a/exercises/21_runtime_datatype/main.cpp b/exercises/21_runtime_datatype/main.cpp index 9c4bf376a..758bb3958 100644 --- a/exercises/21_runtime_datatype/main.cpp +++ b/exercises/21_runtime_datatype/main.cpp @@ -18,13 +18,27 @@ struct TaggedUnion { }; // TODO: 将这个函数模板化用于 sigmoid_dyn -float sigmoid(float x) { +template T sigmoid(T x) { return 1 / (1 + std::exp(-x)); } TaggedUnion sigmoid_dyn(TaggedUnion x) { TaggedUnion ans{x.type}; // TODO: 根据 type 调用 sigmoid + switch (x.type) { + case DataType::Float: + // 输入是float类型,调用sigmoid,结果存入ans.f + ans.f = sigmoid(x.f); + break; + case DataType::Double: + // 输入是double类型,调用sigmoid,结果存入ans.d + ans.d = sigmoid(x.d); + break; + // 防御性处理:理论上不会走到这里,避免未初始化 + default: + ASSERT(false, "Unsupported data type"); + } + return ans; } diff --git a/exercises/22_class_template/main b/exercises/22_class_template/main new file mode 100755 index 000000000..a4dce25ef Binary files /dev/null and b/exercises/22_class_template/main differ diff --git a/exercises/22_class_template/main.cpp b/exercises/22_class_template/main.cpp index d4985d904..1407d49b9 100644 --- a/exercises/22_class_template/main.cpp +++ b/exercises/22_class_template/main.cpp @@ -10,6 +10,11 @@ struct Tensor4D { Tensor4D(unsigned int const shape_[4], T const *data_) { unsigned int size = 1; // TODO: 填入正确的 shape 并计算 size + for (int i = 0; i < 4; ++i) { + shape[i] = shape_[i]; + // 2. 计算总元素数:4个维度的乘积 + size *= shape[i]; + } data = new T[size]; std::memcpy(data, data_, size * sizeof(T)); } @@ -27,7 +32,43 @@ struct Tensor4D { // 例如,`this` 形状为 `[1, 2, 3, 4]`,`others` 形状为 `[1, 2, 1, 4]`, // 则 `this` 与 `others` 相加时,3 个形状为 `[1, 2, 1, 4]` 的子张量各自与 `others` 对应项相加。 Tensor4D &operator+=(Tensor4D const &others) { - // TODO: 实现单向广播的加法 + unsigned int strides[4]; + strides[3] = 1; + strides[2] = shape[3]; + strides[1] = shape[2] * shape[3]; + strides[0] = shape[1] * shape[2] * shape[3]; + + unsigned int other_strides[4]; + other_strides[3] = 1; + other_strides[2] = others.shape[3]; + other_strides[1] = others.shape[2] * others.shape[3]; + other_strides[0] = others.shape[1] * others.shape[2] * others.shape[3]; + + // 遍历所有元素进行加法 + for (unsigned int i = 0; i < shape[0]; ++i) { + for (unsigned int j = 0; j < shape[1]; ++j) { + for (unsigned int k = 0; k < shape[2]; ++k) { + for (unsigned int l = 0; l < shape[3]; ++l) { + // 计算当前元素在data中的索引 + unsigned int idx = i * strides[0] + j * strides[1] + k * strides[2] + l; + + // 计算other中对应元素的索引(考虑广播) + unsigned int other_i = (others.shape[0] == 1) ? 0 : i; + unsigned int other_j = (others.shape[1] == 1) ? 0 : j; + unsigned int other_k = (others.shape[2] == 1) ? 0 : k; + unsigned int other_l = (others.shape[3] == 1) ? 0 : l; + + unsigned int other_idx = other_i * other_strides[0] + + other_j * other_strides[1] + + other_k * other_strides[2] + + other_l; + + data[idx] += others.data[other_idx]; + } + } + } + } + return *this; } }; @@ -46,8 +87,8 @@ int main(int argc, char **argv) { 17, 18, 19, 20, 21, 22, 23, 24}; // clang-format on - auto t0 = Tensor4D(shape, data); - auto t1 = Tensor4D(shape, data); + Tensor4D t0(shape, data); + Tensor4D t1(shape, data); t0 += t1; for (auto i = 0u; i < sizeof(data) / sizeof(*data); ++i) { ASSERT(t0.data[i] == data[i] * 2, "Tensor doubled by plus its self."); @@ -77,8 +118,8 @@ int main(int argc, char **argv) { 1}; // clang-format on - auto t0 = Tensor4D(s0, d0); - auto t1 = Tensor4D(s1, d1); + Tensor4D t0(s0, d0); + Tensor4D t1(s1, d1); t0 += t1; for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) { ASSERT(t0.data[i] == 7.f, "Every element of t0 should be 7 after adding t1 to it."); @@ -99,8 +140,8 @@ int main(int argc, char **argv) { unsigned int s1[]{1, 1, 1, 1}; double d1[]{1}; - auto t0 = Tensor4D(s0, d0); - auto t1 = Tensor4D(s1, d1); + Tensor4D t0(s0, d0); + Tensor4D t1(s1, d1); t0 += t1; for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) { ASSERT(t0.data[i] == d0[i] + 1, "Every element of t0 should be incremented by 1 after adding t1 to it."); diff --git a/exercises/23_template_const/main b/exercises/23_template_const/main new file mode 100755 index 000000000..280117b84 Binary files /dev/null and b/exercises/23_template_const/main differ diff --git a/exercises/23_template_const/main.cpp b/exercises/23_template_const/main.cpp index e0105e168..b1bf8a5ed 100644 --- a/exercises/23_template_const/main.cpp +++ b/exercises/23_template_const/main.cpp @@ -11,6 +11,10 @@ struct Tensor { Tensor(unsigned int const shape_[N]) { unsigned int size = 1; // TODO: 填入正确的 shape 并计算 size + for (unsigned int i = 0; i < N; ++i) { + shape[i] = shape_[i]; + size *= shape[i]; + } data = new T[size]; std::memset(data, 0, size * sizeof(T)); } @@ -35,6 +39,11 @@ struct Tensor { for (unsigned int i = 0; i < N; ++i) { ASSERT(indices[i] < shape[i], "Invalid index"); // TODO: 计算 index + unsigned int stride = 1; + for (unsigned int j = i + 1; j < N; ++j) { + stride *= shape[j]; + } + index += indices[i] * stride; } return index; } @@ -44,7 +53,7 @@ struct Tensor { int main(int argc, char **argv) { { unsigned int shape[]{2, 3, 4, 5}; - auto tensor = Tensor<4, int>(shape); + Tensor<4, int> tensor(shape); unsigned int i0[]{0, 0, 0, 0}; tensor[i0] = 1; @@ -58,7 +67,7 @@ int main(int argc, char **argv) { } { unsigned int shape[]{7, 8, 128}; - auto tensor = Tensor<3, float>(shape); + Tensor<3, float>tensor(shape); unsigned int i0[]{0, 0, 0}; tensor[i0] = 1.f; diff --git a/exercises/24_std_array/main b/exercises/24_std_array/main new file mode 100755 index 000000000..39f36209a Binary files /dev/null and b/exercises/24_std_array/main differ diff --git a/exercises/24_std_array/main.cpp b/exercises/24_std_array/main.cpp index c29718d9d..1130c998f 100644 --- a/exercises/24_std_array/main.cpp +++ b/exercises/24_std_array/main.cpp @@ -8,21 +8,21 @@ int main(int argc, char **argv) { { std::array arr{{1, 2, 3, 4, 5}}; - ASSERT(arr.size() == ?, "Fill in the correct value."); - ASSERT(sizeof(arr) == ?, "Fill in the correct value."); + ASSERT(arr.size() == 5, "Fill in the correct value."); + ASSERT(sizeof(arr) == 20, "Fill in the correct value."); int ans[]{1, 2, 3, 4, 5}; - ASSERT(std::memcmp(arr.?, ans, ?) == 0, "Fill in the correct values."); + ASSERT(std::memcmp(arr.data(), ans, sizeof(ans)) == 0, "Fill in the correct values."); } { std::array arr; - ASSERT(arr.size() == ?, "Fill in the correct value."); - ASSERT(sizeof(arr) == ?, "Fill in the correct value."); + ASSERT(arr.size() == 8, "Fill in the correct value."); + ASSERT(sizeof(arr) == 64, "Fill in the correct value."); } { std::array arr{"Hello, InfiniTensor!"}; - ASSERT(arr.size() == ?, "Fill in the correct value."); - ASSERT(sizeof(arr) == ?, "Fill in the correct value."); - ASSERT(std::strcmp(arr.?, "Hello, InfiniTensor!") == 0, "Fill in the correct value."); + ASSERT(arr.size() == 21, "Fill in the correct value."); + ASSERT(sizeof(arr) == 21, "Fill in the correct value."); + ASSERT(std::strcmp(arr.data(), "Hello, InfiniTensor!") == 0, "Fill in the correct value."); } return 0; } diff --git a/exercises/25_std_vector/main b/exercises/25_std_vector/main new file mode 100755 index 000000000..b4dcdfdc7 Binary files /dev/null and b/exercises/25_std_vector/main differ diff --git a/exercises/25_std_vector/main.cpp b/exercises/25_std_vector/main.cpp index f9e41bb78..01c63ef3f 100644 --- a/exercises/25_std_vector/main.cpp +++ b/exercises/25_std_vector/main.cpp @@ -8,81 +8,81 @@ int main(int argc, char **argv) { { std::vector vec{1, 2, 3, 4, 5}; - ASSERT(vec.size() == ?, "Fill in the correct value."); + ASSERT(vec.size() == 5, "Fill in the correct value."); // THINK: `std::vector` 的大小是什么意思?与什么有关? - ASSERT(sizeof(vec) == ?, "Fill in the correct value."); + ASSERT(sizeof(vec) == 24, "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."); + ASSERT(std::memcmp(vec.data(), ans, sizeof(ans)) == 0, "Fill in the correct values."); } { std::vector vec{1, 2, 3, 4, 5}; { - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(sizeof(vec) == ?, "Fill in the correct value."); + ASSERT(vec.size() == 5, "Fill in the correct value."); + ASSERT(sizeof(vec) == 24, "Fill in the correct value."); double ans[]{1, 2, 3, 4, 5}; - ASSERT(std::memcmp(vec.?, ans, sizeof(ans)) == 0, "Fill in the correct values."); + ASSERT(std::memcmp(vec.data(), ans, sizeof(ans)) == 0, "Fill in the correct values."); } { vec.push_back(6); - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(sizeof(vec) == ?, "Fill in the correct value."); + ASSERT(vec.size() == 6, "Fill in the correct value."); + ASSERT(sizeof(vec) == 24, "Fill in the correct value."); vec.pop_back(); - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(sizeof(vec) == ?, "Fill in the correct value."); + ASSERT(vec.size() == 5, "Fill in the correct value."); + ASSERT(sizeof(vec) == 24, "Fill in the correct value."); } { vec[4] = 6; - ASSERT(vec[0] == ?, "Fill in the correct value."); - ASSERT(vec[1] == ?, "Fill in the correct value."); - ASSERT(vec[2] == ?, "Fill in the correct value."); - ASSERT(vec[3] == ?, "Fill in the correct value."); - ASSERT(vec[4] == ?, "Fill in the correct value."); + ASSERT(vec[0] == 1, "Fill in the correct value."); + ASSERT(vec[1] == 2, "Fill in the correct value."); + ASSERT(vec[2] == 3, "Fill in the correct value."); + ASSERT(vec[3] == 4, "Fill in the correct value."); + ASSERT(vec[4] == 6, "Fill in the correct value."); } { // THINK: `std::vector` 插入删除的时间复杂度是什么? - vec.insert(?, 1.5); + vec.insert(vec.begin() + 1, 1.5); ASSERT((vec == std::vector{1, 1.5, 2, 3, 4, 6}), "Make this assertion pass."); - vec.erase(?); + vec.erase(vec.begin() + 3); ASSERT((vec == std::vector{1, 1.5, 2, 4, 6}), "Make this assertion pass."); } { vec.shrink_to_fit(); - ASSERT(vec.capacity() == ?, "Fill in the correct value."); + ASSERT(vec.capacity() == 5, "Fill in the correct value."); vec.clear(); ASSERT(vec.empty(), "`vec` is empty now."); - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(vec.capacity() == ?, "Fill in the correct value."); + ASSERT(vec.size() == 0, "Fill in the correct value."); + ASSERT(vec.capacity() == 5, "Fill in the correct value."); } } { - std::vector vec(?, ?); // TODO: 调用正确的构造函数 + std::vector vec(48, 'z'); // TODO: 调用正确的构造函数 ASSERT(vec[0] == 'z', "Make this assertion pass."); ASSERT(vec[47] == 'z', "Make this assertion pass."); ASSERT(vec.size() == 48, "Make this assertion pass."); - ASSERT(sizeof(vec) == ?, "Fill in the correct value."); + ASSERT(sizeof(vec) == 24, "Fill in the correct value."); { auto capacity = vec.capacity(); vec.resize(16); - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(vec.capacity() == ?, "Fill in a correct identifier."); + ASSERT(vec.size() == 16, "Fill in the correct value."); + ASSERT(vec.capacity() == 48, "Fill in a correct identifier."); } { vec.reserve(256); - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(vec.capacity() == ?, "Fill in the correct value."); + ASSERT(vec.size() == 16, "Fill in the correct value."); + ASSERT(vec.capacity() == 256, "Fill in the correct value."); } { vec.push_back('a'); vec.push_back('b'); vec.push_back('c'); vec.push_back('d'); - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(vec.capacity() == ?, "Fill in the correct value."); - ASSERT(vec[15] == ?, "Fill in the correct value."); - ASSERT(vec[?] == 'a', "Fill in the correct value."); - ASSERT(vec[?] == 'b', "Fill in the correct value."); - ASSERT(vec[?] == 'c', "Fill in the correct value."); - ASSERT(vec[?] == 'd', "Fill in the correct value."); + ASSERT(vec.size() == 20, "Fill in the correct value."); + ASSERT(vec.capacity() == 256, "Fill in the correct value."); + ASSERT(vec[15] == 'z', "Fill in the correct value."); + ASSERT(vec[16] == 'a', "Fill in the correct value."); + ASSERT(vec[17] == 'b', "Fill in the correct value."); + ASSERT(vec[18] == 'c', "Fill in the correct value."); + ASSERT(vec[19] == 'd', "Fill in the correct value."); } } diff --git a/exercises/26_std_vector_bool/main b/exercises/26_std_vector_bool/main new file mode 100755 index 000000000..98a6ce297 Binary files /dev/null and b/exercises/26_std_vector_bool/main differ diff --git a/exercises/26_std_vector_bool/main.cpp b/exercises/26_std_vector_bool/main.cpp index b4ab4f9c4..375a9aeea 100644 --- a/exercises/26_std_vector_bool/main.cpp +++ b/exercises/26_std_vector_bool/main.cpp @@ -6,29 +6,33 @@ // TODO: 将下列 `?` 替换为正确的代码 int main(int argc, char **argv) { - std::vector vec(?, ?);// TODO: 正确调用构造函数 +<<<<<<< HEAD + std::vector vec(100, 1);// TODO: 正确调用构造函数 +======= + std::vector vec(100, true);// TODO: 正确调用构造函数 +>>>>>>> 9e612d870243a6cbd2998304484a0d6720e5e46f ASSERT(vec[0], "Make this assertion pass."); ASSERT(vec[99], "Make this assertion pass."); ASSERT(vec.size() == 100, "Make this assertion pass."); // NOTICE: 平台相关!注意 CI:Ubuntu 上的值。 std::cout << "sizeof(std::vector) = " << sizeof(std::vector) << std::endl; - ASSERT(sizeof(vec) == ?, "Fill in the correct value."); + ASSERT(sizeof(vec) == 40, "Fill in the correct value."); { vec[20] = false; - ASSERT(?vec[20], "Fill in `vec[20]` or `!vec[20]`."); + ASSERT(!vec[20], "Fill in `vec[20]` or `!vec[20]`."); } { vec.push_back(false); - ASSERT(vec.size() == ?, "Fill in the correct value."); - ASSERT(?vec[100], "Fill in `vec[100]` or `!vec[100]`."); + ASSERT(vec.size() == 101, "Fill in the correct value."); + ASSERT(!vec[100], "Fill in `vec[100]` or `!vec[100]`."); } { auto ref = vec[30]; - ASSERT(?ref, "Fill in `ref` or `!ref`"); + ASSERT(ref, "Fill in `ref` or `!ref`"); ref = false; - ASSERT(?ref, "Fill in `ref` or `!ref`"); + ASSERT(!ref, "Fill in `ref` or `!ref`"); // THINK: WHAT and WHY? - ASSERT(?vec[30], "Fill in `vec[30]` or `!vec[30]`."); + ASSERT(!vec[30], "Fill in `vec[30]` or `!vec[30]`."); } return 0; } diff --git a/exercises/27_strides/main b/exercises/27_strides/main new file mode 100755 index 000000000..20e1f8d68 Binary files /dev/null and b/exercises/27_strides/main differ diff --git a/exercises/27_strides/main.cpp b/exercises/27_strides/main.cpp index baceaf2a9..06d25f04f 100644 --- a/exercises/27_strides/main.cpp +++ b/exercises/27_strides/main.cpp @@ -18,6 +18,15 @@ std::vector strides(std::vector const &shape) { // TODO: 完成函数体,根据张量形状计算张量连续存储时的步长。 // READ: 逆向迭代器 std::vector::rbegin // 使用逆向迭代器可能可以简化代码 + udim current_stride = 1; // 最后一维的步长固定为1 + auto shape_it = shape.rbegin(); // shape的逆向开始(最后一个元素) + auto stride_it = strides.rbegin(); // 结果的逆向开始(最后一个位置) + while (shape_it != shape.rend() && stride_it != strides.rend()) { + *stride_it = current_stride; // 给当前维度赋值步长 + current_stride *= *shape_it; // 计算前一维的步长(当前步长 × 当前维度形状) + ++shape_it; + ++stride_it; + } return strides; } diff --git a/exercises/28_std_string/main b/exercises/28_std_string/main new file mode 100755 index 000000000..9988f6604 Binary files /dev/null and b/exercises/28_std_string/main differ diff --git a/exercises/28_std_string/main.cpp b/exercises/28_std_string/main.cpp index d8b276274..d5b1eb909 100644 --- a/exercises/28_std_string/main.cpp +++ b/exercises/28_std_string/main.cpp @@ -10,9 +10,9 @@ int main(int argc, char **argv) { auto world = "world"; // READ: `decltype` 表达式 // READ: `std::is_same_v` 元编程判别 - ASSERT((std::is_same_v), "Fill in the missing type."); - ASSERT((std::is_same_v), "Fill in the missing type."); + ASSERT((std::is_same::value), "Fill in the missing type."); + ASSERT((std::is_same::value), "Fill in the missing type."); // TODO: 将 `?` 替换为正确的字符串 - ASSERT(hello + ", " + world + '!' == "?", "Fill in the missing string."); + ASSERT(hello + ", " + world + '!' == "Hello, world!", "Fill in the missing string."); return 0; } diff --git a/exercises/29_std_map/main b/exercises/29_std_map/main new file mode 100755 index 000000000..572bc0459 Binary files /dev/null and b/exercises/29_std_map/main differ diff --git a/exercises/29_std_map/main.cpp b/exercises/29_std_map/main.cpp index fcccca347..b8c3a26db 100644 --- a/exercises/29_std_map/main.cpp +++ b/exercises/29_std_map/main.cpp @@ -7,11 +7,14 @@ template bool key_exists(std::map const &map, k const &key) { // TODO: 实现函数 + return map.find(key) != map.end(); } template void set(std::map &map, k key, v value) { // TODO: 实现函数 + map[key] = value; + } // ---- 不要修改以下代码 ---- diff --git a/exercises/30_std_unique_ptr/main b/exercises/30_std_unique_ptr/main new file mode 100755 index 000000000..ee99f737b Binary files /dev/null and b/exercises/30_std_unique_ptr/main differ diff --git a/exercises/30_std_unique_ptr/main.cpp b/exercises/30_std_unique_ptr/main.cpp index 9b98b5794..0c480a6ed 100644 --- a/exercises/30_std_unique_ptr/main.cpp +++ b/exercises/30_std_unique_ptr/main.cpp @@ -53,14 +53,15 @@ int main(int argc, char **argv) { {"fd"}, // TODO: 分析 problems[1] 中资源的生命周期,将记录填入 `std::vector` // NOTICE: 此题结果依赖对象析构逻辑,平台相关,提交时以 CI 实际运行平台为准 - {"", "", "", "", "", "", "", ""}, - {"", "", "", "", "", "", "", ""}, + {"d", "ffr"}, // problems[1] + {"d", "d", "r"} // problems[2] }; // ---- 不要修改以下代码 ---- for (auto i = 0; i < 3; ++i) { ASSERT(problems[i].size() == answers[i].size(), "wrong size"); + for (auto j = 0; j < problems[i].size(); ++j) { ASSERT(std::strcmp(problems[i][j].c_str(), answers[i][j]) == 0, "wrong location"); } diff --git a/exercises/31_std_shared_ptr/main b/exercises/31_std_shared_ptr/main new file mode 100755 index 000000000..f7a2542ce Binary files /dev/null and b/exercises/31_std_shared_ptr/main differ diff --git a/exercises/31_std_shared_ptr/main.cpp b/exercises/31_std_shared_ptr/main.cpp index febbbcc6f..79993b19e 100644 --- a/exercises/31_std_shared_ptr/main.cpp +++ b/exercises/31_std_shared_ptr/main.cpp @@ -10,36 +10,38 @@ int main(int argc, char **argv) { std::shared_ptr ptrs[]{shared, shared, shared}; std::weak_ptr observer = shared; - ASSERT(observer.use_count() == ?, ""); + ASSERT(observer.use_count() == 4, ""); ptrs[0].reset(); - ASSERT(observer.use_count() == ?, ""); + ASSERT(observer.use_count() == 3, ""); ptrs[1] = nullptr; - ASSERT(observer.use_count() == ?, ""); + ASSERT(observer.use_count() == 2, ""); ptrs[2] = std::make_shared(*shared); - ASSERT(observer.use_count() == ?, ""); + ASSERT(observer.use_count() == 1, ""); ptrs[0] = shared; ptrs[1] = shared; ptrs[2] = std::move(shared); - ASSERT(observer.use_count() == ?, ""); + ASSERT(observer.use_count() == 3, ""); std::ignore = std::move(ptrs[0]); ptrs[1] = std::move(ptrs[1]); ptrs[1] = std::move(ptrs[2]); - ASSERT(observer.use_count() == ?, ""); + + ASSERT(observer.use_count() == 2, ""); shared = observer.lock(); - ASSERT(observer.use_count() == ?, ""); + + ASSERT(observer.use_count() == 3, ""); shared = nullptr; for (auto &ptr : ptrs) ptr = nullptr; - ASSERT(observer.use_count() == ?, ""); + ASSERT(observer.use_count() == 0, ""); shared = observer.lock(); - ASSERT(observer.use_count() == ?, ""); + ASSERT(observer.use_count() == 0, ""); return 0; } diff --git a/exercises/32_std_transform/main b/exercises/32_std_transform/main new file mode 100755 index 000000000..b2abb8b47 Binary files /dev/null and b/exercises/32_std_transform/main differ diff --git a/exercises/32_std_transform/main.cpp b/exercises/32_std_transform/main.cpp index f4dc25a5c..e01042171 100644 --- a/exercises/32_std_transform/main.cpp +++ b/exercises/32_std_transform/main.cpp @@ -9,7 +9,15 @@ int main(int argc, char **argv) { std::vector val{8, 13, 21, 34, 55}; // TODO: 调用 `std::transform`,将 `v` 中的每个元素乘以 2,并转换为字符串,存入 `ans` - // std::vector ans + std::vector ans(val.size()); + std::transform( + val.begin(), // 输入序列起始迭代器 + val.end(), // 输入序列结束迭代器 + ans.begin(), // 输出序列起始迭代器 + [](int n) { // 一元操作函数(lambda表达式) + return std::to_string(n * 2); // 先乘2,再转为字符串 + } + ); 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"); diff --git a/exercises/33_std_accumulate/main b/exercises/33_std_accumulate/main new file mode 100755 index 000000000..9f152cc83 Binary files /dev/null and b/exercises/33_std_accumulate/main differ diff --git a/exercises/33_std_accumulate/main.cpp b/exercises/33_std_accumulate/main.cpp index 6326929d5..3715d4bb1 100644 --- a/exercises/33_std_accumulate/main.cpp +++ b/exercises/33_std_accumulate/main.cpp @@ -12,6 +12,15 @@ int main(int argc, char **argv) { // - 连续存储; // 的张量占用的字节数 // int size = + float element_count = std::accumulate( + std::begin(shape), // 输入序列起始迭代器(数组首地址) + std::end(shape), // 输入序列结束迭代器(数组尾后地址) + 1.0f, // 初始值(float类型,避免整数溢出) + std::multiplies() // 累乘操作(替代默认的累加) + ); + + // 步骤2:计算总字节数 = 总元素数 × 单个元素的字节数 + int size = static_cast(element_count * sizeof(DataType)); ASSERT(size == 602112, "4x1x3x224x224 = 602112"); return 0; }