mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 14:03:50 +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:
@@ -12,25 +12,25 @@ public class recursion {
|
||||
// 終了条件
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
int res = Recur(n - 1);
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* 反復で再帰を模擬する */
|
||||
/* 反復を使って再帰を再現する */
|
||||
int ForLoopRecur(int n) {
|
||||
// 明示的なスタックを使ってシステムコールスタックを模擬する
|
||||
// 明示的なスタックを使って、システムの呼び出しスタックを再現する
|
||||
Stack<int> stack = new();
|
||||
int res = 0;
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
for (int i = n; i > 0; i--) {
|
||||
// 「スタックへのプッシュ」で「再帰」を模擬する
|
||||
// 「プッシュ操作」で再帰の「往路」を再現する
|
||||
stack.Push(i);
|
||||
}
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
while (stack.Count > 0) {
|
||||
// 「スタックから取り出す操作」で「帰り」をシミュレート
|
||||
// 「ポップ操作」で再帰の「復路」を再現する
|
||||
res += stack.Pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
@@ -51,26 +51,26 @@ public class recursion {
|
||||
// 終了条件 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 */
|
||||
/* 実行用コード */
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = Recur(n);
|
||||
Console.WriteLine("\n再帰関数の合計結果 res = " + res);
|
||||
Console.WriteLine("\n再帰関数で求めた和 res = " + res);
|
||||
|
||||
res = ForLoopRecur(n);
|
||||
Console.WriteLine("\n反復で再帰をシミュレートした合計結果 res = " + res);
|
||||
Console.WriteLine("\n反復で再帰を再現して求めた和 res = " + res);
|
||||
|
||||
res = TailRecur(n, 0);
|
||||
Console.WriteLine("\n末尾再帰関数の合計結果 res = " + res);
|
||||
Console.WriteLine("\n末尾再帰関数で求めた和 res = " + res);
|
||||
|
||||
res = Fib(n);
|
||||
Console.WriteLine("\nフィボナッチ数列の第 " + n + " 項は " + res);
|
||||
|
||||
Reference in New Issue
Block a user