Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/00_hello_world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions exercises/01_variable&add/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
int main(int argc, char **argv) {
// TODO: 补全变量定义并打印加法运算
// x ?
int x=10;
std::cout << x << " + " << x << " = " << x + x << std::endl;
return 0;
}
2 changes: 2 additions & 0 deletions exercises/02_function/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// NOTICE: 补充由内而外读法的机翻解释 <https://learn.microsoft.com/zh-cn/cpp/c-language/interpreting-more-complex-declarators?view=msvc-170>

// 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");
Expand All @@ -16,4 +17,5 @@ int main(int argc, char **argv) {

int add(int a, int b) {
// TODO: 补全函数定义,但不要移动代码行
return a+b;
}
8 changes: 4 additions & 4 deletions exercises/03_argument&parameter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 99");
std::cout << "befor func call: " << arg << std::endl;
func(arg);
ASSERT(arg == ?, "arg should be ?");
ASSERT(arg == 99, "arg should be 99");
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 99");
std::cout << "befor add: " << param << std::endl;
param += 1;
ASSERT(param == ?, "param should be ?");
ASSERT(param == 100, "param should be 100");
std::cout << "after add: " << param << std::endl;
}
10 changes: 5 additions & 5 deletions exercises/04_static/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions exercises/05_constexpr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ int main(int argc, char **argv) {

// TODO: 观察错误信息,修改一处,使代码编译运行
// PS: 编译运行,但是不一定能算出结果……
constexpr auto ANS_N = 90;
constexpr auto ANS = fibonacci(ANS_N);
constexpr auto ANS_N = 10;
//constexpr auto ANS = fibonacci(ANS_N);
auto ANS = fibonacci(ANS_N);
std::cout << "fibonacci(" << ANS_N << ") = " << ANS << std::endl;

return 0;
Expand Down
6 changes: 4 additions & 2 deletions exercises/06_array/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ unsigned long long fibonacci(int i) {
return 1;
default:
// TODO: 补全三目表达式缺失的部分
return <condition> ? <cache> : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2));
//return <condition> ? <cache> : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2));
return arr[i] ? arr[i] : (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) == ?, "sizeof array is size of all its elements");
ASSERT(sizeof(arr) == sizeof(unsigned long long) * 90, "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");
Expand Down
12 changes: 10 additions & 2 deletions exercises/07_loop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
// READ: 纯函数 <https://zh.wikipedia.org/wiki/%E7%BA%AF%E5%87%BD%E6%95%B0>
static unsigned long long fibonacci(int i) {
// TODO: 为缓存设置正确的初始值
static unsigned long long cache[96], cached;
static unsigned long long cache[96] = {0, 1}; // 缓存数组,第 0 项为 0,第 1 项为 1
static int cached = 2; // 即将计算但尚未计算的索引
// TODO: 设置正确的循环条件
for (; false; ++cached) {

// 如果缓存中已经有计算过的结果
if (i < cached) {
return cache[i];
}

// 如果缓存不足,计算 Fibonacci 数
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
9 changes: 9 additions & 0 deletions exercises/08_pointer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ 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]
for (int i = 0; i < len - 2; ++i) {
int a = *(ptr + i * stride);
int b = *(ptr + (i + 1) * stride);
int c = *(ptr + (i + 2) * stride);

if (c != a + b) {
return false;
}
}
return true;
}

Expand Down
1 change: 1 addition & 0 deletions exercises/09_enum&union/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ColorEnum convert_by_pun(Color c) {

TypePun pun;
// TODO: 补全类型双关转换
pun.c = c;

return pun.e;
}
Expand Down
37 changes: 35 additions & 2 deletions exercises/10_trivial/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,55 @@

struct FibonacciCache {
unsigned long long cache[16];
int cached;
int cached; // 最早的尚未被计算器的位置索引
};

/*
// TODO: 实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(FibonacciCache &cache, int i) {

for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache.cache[i];
}
*/
// TODO: 实现正确的缓存优化斐波那契计算
static unsigned long long fibonacci(FibonacciCache &cache, int i) {
// 如果请求的斐波那契数已经在缓存中,直接返回
if (i < cache.cached) {
return cache.cache[i];
}

if (cache.cached < 2) {
cache.cache[0] = 0;
cache.cache[1] = 1;
cache.cached = 2;
}

// 否则,计算并缓存从 cache.cached 到 i 的斐波那契数
for (int j = cache.cached; j <= i; ++j) {
if (j == 0) {
cache.cache[j] = 0;
} else if (j == 1) {
cache.cache[j] = 1;
} else {
cache.cache[j] = cache.cache[j - 1] + cache.cache[j - 2];
}
}

// 更新缓存中已经计算过的斐波那契数的个数
cache.cached = i + 1;

// 返回请求的斐波那契数
return cache.cache[i];
}

int main(int argc, char **argv) {
// TODO: 初始化缓存结构体,使计算正确
// NOTICE: C/C++ 中,读取未初始化的变量(包括结构体变量)是未定义行为
// READ: 初始化的各种写法 <https://zh.cppreference.com/w/cpp/language/initialization>
FibonacciCache fib;
FibonacciCache fib{};
ASSERT(fibonacci(fib, 10) == 55, "fibonacci(10) should be 55");
std::cout << "fibonacci(10) = " << fibonacci(fib, 10) << std::endl;
return 0;
Expand Down
32 changes: 32 additions & 0 deletions exercises/11_method/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,44 @@ struct Fibonacci {
unsigned long long cache[128];
int cached;

// 构造函数:初始化缓存
Fibonacci() {
// 假设 cache[0] = 0, cache[1] = 1
cache[0] = 0;
cache[1] = 1;
cached = 2; // 目前缓存了前两个数
}

// TODO: 实现正确的缓存优化斐波那契计算
unsigned long long get(int i) {

/*
for (; false; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
*/

if (i < cached) {
return cache[i];
}

// 否则,计算并缓存从 cached 到 i 的斐波那契数
for (int j = cached; j <= i; ++j) {
if (j == 0) {
cache[j] = 0;
} else if (j == 1) {
cache[j] = 1;
} else {
cache[j] = cache[j - 1] + cache[j - 2];
}
}

// 更新缓存中已经计算过的斐波那契数的个数
cached = i + 1;

// 返回请求的斐波那契数
return cache[i];
}
};

Expand Down
3 changes: 2 additions & 1 deletion exercises/12_method_const/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
struct Fibonacci {
int numbers[11];
// TODO: 修改方法签名和实现,使测试通过
int get(int i) {
constexpr int get(int i) const {
return numbers[i];
}
};

Expand Down
9 changes: 8 additions & 1 deletion exercises/13_class/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ class Fibonacci {
public:
// TODO: 实现构造器
// Fibonacci()
Fibonacci() {
cache[0] = 0;
cache[1] = 1;
cached = 2;
}

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
if(i<cached)
return cache[i];
for (; cached<=i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
11 changes: 8 additions & 3 deletions exercises/14_class_destruct/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ class DynFibonacci {

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}
DynFibonacci(int capacity): cache(new size_t[capacity+1]), cached(2) {
cache[0]=0;
cache[1]=1;
}

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();
~DynFibonacci() {
delete[] cache;
}

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
for (; cached<=i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
20 changes: 15 additions & 5 deletions exercises/15_class_clone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@

class DynFibonacci {
size_t *cache;
int cached;
int cached; // 目前第一个尚未计算的索引
// 我自己新增的
int capacity; // 新增成员变量记录总容量

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}
DynFibonacci(int capacity): cache(new size_t[capacity]), cached(2), capacity(capacity) {
cache[0]=0;
cache[1]=1;
}

// TODO: 实现复制构造器
DynFibonacci(DynFibonacci const &) = delete;
DynFibonacci(DynFibonacci const &other)
: cache(new size_t[other.capacity]), cached(other.cached), capacity(other.capacity) {
std::copy(other.cache, other.cache + other.cached, this->cache);
}

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();
~DynFibonacci() {
delete[] cache;
}

// TODO: 实现正确的缓存优化斐波那契计算
size_t get(int i) {
for (; false; ++cached) {
for (; cached<=i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
Expand Down
42 changes: 29 additions & 13 deletions exercises/16_class_move/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,50 @@ class DynFibonacci {
int cached;

public:
// TODO: 实现动态设置容量的构造器
DynFibonacci(int capacity): cache(new ?), cached(?) {}
// 动态设置容量的构造器
DynFibonacci(int capacity) : cache(new size_t[capacity]), cached(1) {
cache[0] = 0;
cache[1] = 1;
}

// TODO: 实现移动构造器
DynFibonacci(DynFibonacci &&) noexcept = delete;
// 移动构造器
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) {
while (cached < i) {
++cached;
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}

// NOTICE: 不要修改这个方法
size_t operator[](int i) const {
ASSERT(i <= cached, "i out of range");
return cache[i];
}

// NOTICE: 不要修改这个方法
bool is_alive() const {
return cache;
}
Expand Down
Loading