mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-08 23:20:05 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -11,25 +11,25 @@ int recur(int n) {
|
||||
// 終了条件
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
int res = recur(n - 1);
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* 反復で再帰を模擬する */
|
||||
/* 反復を使って再帰を再現する */
|
||||
int forLoopRecur(int n) {
|
||||
// 明示的なスタックを使ってシステムコールスタックを模擬する
|
||||
// 明示的なスタックを使って、システムの呼び出しスタックを再現する
|
||||
stack<int> stack;
|
||||
int res = 0;
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
for (int i = n; i > 0; i--) {
|
||||
// 「スタックへのプッシュ」で「再帰」を模擬する
|
||||
// 「プッシュ操作」で再帰の「往路」を再現する
|
||||
stack.push(i);
|
||||
}
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
while (!stack.empty()) {
|
||||
// 「スタックから取り出す操作」で「帰り」をシミュレート
|
||||
// 「ポップ操作」で再帰の「復路」を再現する
|
||||
res += stack.top();
|
||||
stack.pop();
|
||||
}
|
||||
@@ -51,25 +51,25 @@ 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) を再帰的に呼び出す
|
||||
// 再帰呼び出し f(n) = f(n-1) + f(n-2)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// 結果 f(n) を返す
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
cout << "\n再帰関数の合計結果 res = " << res << endl;
|
||||
cout << "\n再帰関数で求めた和 res = " << res << endl;
|
||||
|
||||
res = forLoopRecur(n);
|
||||
cout << "\n反復で再帰をシミュレートした合計結果 res = " << res << endl;
|
||||
cout << "\n反復で再帰を再現して求めた和 res = " << res << endl;
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
cout << "\n末尾再帰関数の合計結果 res = " << res << endl;
|
||||
cout << "\n末尾再帰関数で求めた和 res = " << res << endl;
|
||||
|
||||
res = fib(n);
|
||||
cout << "\nフィボナッチ数列の第 " << n << " 項は " << res << endl;
|
||||
|
||||
Reference in New Issue
Block a user