mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 06:10:19 +08:00
build
This commit is contained in:
@@ -158,7 +158,15 @@ status: new
|
||||
=== "C"
|
||||
|
||||
```c title="iteration.c"
|
||||
[class]{}-[func]{forLoop}
|
||||
/* for 循环 */
|
||||
int forLoop(int n) {
|
||||
int res = 0;
|
||||
// 循环求和 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -344,7 +352,17 @@ status: new
|
||||
=== "C"
|
||||
|
||||
```c title="iteration.c"
|
||||
[class]{}-[func]{whileLoop}
|
||||
/* while 循环 */
|
||||
int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 初始化条件变量
|
||||
// 循环求和 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // 更新条件变量
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -539,7 +557,19 @@ status: new
|
||||
=== "C"
|
||||
|
||||
```c title="iteration.c"
|
||||
[class]{}-[func]{whileLoopII}
|
||||
/* while 循环(两次更新) */
|
||||
int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 初始化条件变量
|
||||
// 循环求和 1, 4, ...
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 更新条件变量
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -724,7 +754,23 @@ status: new
|
||||
=== "C"
|
||||
|
||||
```c title="iteration.c"
|
||||
[class]{}-[func]{nestedForLoop}
|
||||
/* 双层 for 循环 */
|
||||
char *nestedForLoop(int n) {
|
||||
// n * n 为对应点数量,"(i, j), " 对应字符串长最大为 6+10*2,加上最后一个空字符 \0 的额外空间
|
||||
int size = n * n * 26 + 1;
|
||||
char *res = malloc(size * sizeof(char));
|
||||
|
||||
// 循环 i = 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
// 循环 j = 1, 2, ..., n-1, n
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
char tmp[26];
|
||||
snprintf(tmp, sizeof(tmp), "(%d, %d), ", i, j);
|
||||
strncat(res, tmp, size - strlen(res) - 1);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -910,7 +956,16 @@ status: new
|
||||
=== "C"
|
||||
|
||||
```c title="recursion.c"
|
||||
[class]{}-[func]{recur}
|
||||
/* 递归 */
|
||||
int recur(int n) {
|
||||
// 终止条件
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 递:递归调用
|
||||
int res = recur(n - 1);
|
||||
// 归:返回结果
|
||||
return n + res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -1091,7 +1146,14 @@ status: new
|
||||
=== "C"
|
||||
|
||||
```c title="recursion.c"
|
||||
[class]{}-[func]{tailRecur}
|
||||
/* 尾递归 */
|
||||
int tailRecur(int n, int res) {
|
||||
// 终止条件
|
||||
if (n == 0)
|
||||
return res;
|
||||
// 尾递归调用
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -1278,7 +1340,16 @@ status: new
|
||||
=== "C"
|
||||
|
||||
```c title="recursion.c"
|
||||
[class]{}-[func]{fib}
|
||||
/* 斐波那契数列:递归 */
|
||||
int fib(int n) {
|
||||
// 终止条件 f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
return n - 1;
|
||||
// 递归调用 f(n) = f(n-1) + f(n-2)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// 返回结果 f(n)
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user