This commit is contained in:
krahets
2023-10-17 20:29:28 +08:00
parent 44ebc7748b
commit de9bf5a57a
5 changed files with 44 additions and 8 deletions
@@ -1632,7 +1632,24 @@ status: new
=== "C"
```c title="recursion.c"
[class]{}-[func]{forLoopRecur}
/* 使用迭代模拟递归 */
int forLoopRecur(int n) {
int stack[1000]; // 借助一个大数组来模拟栈
int top = 0;
int res = 0;
// 递:递归调用
for (int i = n; i > 0; i--) {
// 通过“入栈操作”模拟“递”
stack[top++] = i;
}
// 归:返回结果
while (top >= 0) {
// 通过“出栈操作”模拟“归”
res += stack[top--];
}
// res = 1+2+3+...+n
return res;
}
```
=== "Zig"
@@ -1027,7 +1027,11 @@ $$
=== "Zig"
```zig title="space_complexity.zig"
[class]{}-[func]{function}
// 函数
fn function() i32 {
// 执行某些操作
return 0;
}
// 常数阶
fn constant(n: i32) void {