|
|
|
@ -102,7 +102,15 @@ status: new
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="iteration.dart"
|
|
|
|
|
[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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
@ -216,7 +224,17 @@ status: new
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="iteration.dart"
|
|
|
|
|
[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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
@ -326,7 +344,19 @@ status: new
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="iteration.dart"
|
|
|
|
|
[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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
@ -434,7 +464,18 @@ status: new
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="iteration.dart"
|
|
|
|
|
[class]{}-[func]{nestedForLoop}
|
|
|
|
|
/* 双层 for 循环 */
|
|
|
|
|
String nestedForLoop(int n) {
|
|
|
|
|
String res = "";
|
|
|
|
|
// 循环 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++) {
|
|
|
|
|
res += "($i, $j), ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
@ -557,7 +598,15 @@ status: new
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="recursion.dart"
|
|
|
|
|
[class]{}-[func]{recur}
|
|
|
|
|
/* 递归 */
|
|
|
|
|
int recur(int n) {
|
|
|
|
|
// 终止条件
|
|
|
|
|
if (n == 1) return 1;
|
|
|
|
|
// 递:递归调用
|
|
|
|
|
int res = recur(n - 1);
|
|
|
|
|
// 归:返回结果
|
|
|
|
|
return n + res;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
@ -689,7 +738,13 @@ status: new
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="recursion.dart"
|
|
|
|
|
[class]{}-[func]{tailRecur}
|
|
|
|
|
/* 尾递归 */
|
|
|
|
|
int tailRecur(int n, int res) {
|
|
|
|
|
// 终止条件
|
|
|
|
|
if (n == 0) return res;
|
|
|
|
|
// 尾递归调用
|
|
|
|
|
return tailRecur(n - 1, res + n);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
@ -813,7 +868,15 @@ status: new
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="recursion.dart"
|
|
|
|
|
[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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|