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
@@ -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$;
呼叫兩次會造成大量重複計算。