This commit is contained in:
krahets
2024-03-31 03:53:04 +08:00
parent 87af663929
commit c23e576da4
68 changed files with 2139 additions and 22 deletions

View File

@@ -182,6 +182,12 @@ The following function uses a `for` loop to perform a summation of $1 + 2 + \dot
}
```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{for_loop}
```
=== "Zig"
```zig title="iteration.zig"
@@ -408,6 +414,12 @@ Below we use a `while` loop to implement the sum $1 + 2 + \dots + n$.
}
```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{while_loop}
```
=== "Zig"
```zig title="iteration.zig"
@@ -649,6 +661,12 @@ For example, in the following code, the condition variable $i$ is updated twice
}
```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{while_loop_ii}
```
=== "Zig"
```zig title="iteration.zig"
@@ -883,6 +901,12 @@ We can nest one loop structure within another. Below is an example using `for` l
}
```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{nested_for_loop}
```
=== "Zig"
```zig title="iteration.zig"
@@ -1112,6 +1136,12 @@ Observe the following code, where simply calling the function `recur(n)` can com
}
```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{recur}
```
=== "Zig"
```zig title="recursion.zig"
@@ -1318,8 +1348,9 @@ For example, in calculating $1 + 2 + \dots + n$, we can make the result variable
=== "Kotlin"
```kotlin title="recursion.kt"
/* Kotlin tailrec 关键词使函数实现尾递归优化 */
/* 尾递归 */
tailrec fun tailRecur(n: Int, res: Int): Int {
// 添加 tailrec 关键词,以开启尾递归优化
// 终止条件
if (n == 0)
return res
@@ -1328,6 +1359,12 @@ For example, in calculating $1 + 2 + \dots + n$, we can make the result variable
}
```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{tail_recur}
```
=== "Zig"
```zig title="recursion.zig"
@@ -1554,6 +1591,12 @@ Using the recursive relation, and considering the first two numbers as terminati
}
```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{fib}
```
=== "Zig"
```zig title="recursion.zig"
@@ -1890,6 +1933,12 @@ Therefore, **we can use an explicit stack to simulate the behavior of the call s
}
```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{for_loop_recur}
```
=== "Zig"
```zig title="recursion.zig"