Add multilingual exercise code (#1959)

Replace the exercise pages' Python-only snippets with source-backed implementations for the 13 visible programming languages, and localize reader-facing comments by site language. Zig remains out of scope.

Approval bypass: repository protection requires one approval but does not enforce it for administrators. All 68 completed checks succeeded; four non-required Java jobs remained queued on the existing ubuntu-20.04 workflow, with no failed checks.
This commit is contained in:
Yudong Jin
2026-08-18 04:57:57 +08:00
committed by GitHub
parent bf86c39b6c
commit 28c1e74c1d
180 changed files with 5795 additions and 230 deletions
+5 -11
View File
@@ -25,23 +25,17 @@
下面的递归函数用分治计算 $x^n$:
```python
def fast_pow(x, n):
if n == 0:
return 1
half = fast_pow(x, n // 2)
if n % 2 == 0:
return half * half
return half * half * x
```src
[file]{fast_power}-[class]{}-[func]{fast_pow}
```
用它计算 `fast_pow(3, 5)`
`x = 3``n = 5`,用这个函数计算
<!-- numbered-subquestions -->
1. 递归调用时,参数 `n` 依次变成哪些值?
2. 从最深层开始返回时,各层依次返回什么值?
3. 为什么要先保存 `half`,而不是`fast_pow(x, n // 2)` 写两遍
3. 为什么要先把递归结果保存 `half`,而不是在乘法两边各调用一次相同的子问题
??? success "参考答案"
@@ -50,7 +44,7 @@ def fast_pow(x, n):
2. `n = 0` 时返回 1`n = 1` 时返回 $1×1×3=3$
`n = 2` 时返回 $3×3=9$`n = 5` 时返回 $9×9×3=243$。
3. 如果`fast_pow(x, n // 2)` 在乘法两边各写一次,两次递归会计算完全相同的子问题
3. 如果在乘法两边各调用一次相同的子问题,两次递归会进行完全相同的计算
先把结果保存为 `half`,每层就只递归一次,递归深度约为 $\log n$;
调用两次会造成大量重复计算。