mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 22:42:41 +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:
@@ -10,24 +10,24 @@ fn recur(n: i32) i32 {
|
||||
if (n == 1) {
|
||||
return 1;
|
||||
}
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
const res = recur(n - 1);
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
return n + res;
|
||||
}
|
||||
|
||||
// 反復で再帰を模擬する
|
||||
// 反復を使って再帰を再現する
|
||||
fn forLoopRecur(comptime n: i32) i32 {
|
||||
// 明示的なスタックを使ってシステムコールスタックを模擬する
|
||||
// 明示的なスタックを使って、システムの呼び出しスタックを再現する
|
||||
var stack: [n]i32 = undefined;
|
||||
var res: i32 = 0;
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
var i: usize = n;
|
||||
while (i > 0) {
|
||||
stack[i - 1] = @intCast(i);
|
||||
i -= 1;
|
||||
}
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
var index: usize = n;
|
||||
while (index > 0) {
|
||||
index -= 1;
|
||||
@@ -53,25 +53,25 @@ fn fib(n: i32) i32 {
|
||||
if (n == 1 or n == 2) {
|
||||
return n - 1;
|
||||
}
|
||||
// f(n) = f(n-1) + f(n-2) を再帰的に呼び出す
|
||||
// 再帰呼び出し f(n) = f(n-1) + f(n-2)
|
||||
const res: i32 = fib(n - 1) + fib(n - 2);
|
||||
// 結果 f(n) を返す
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
// 実行用コード
|
||||
pub fn run() void {
|
||||
const n: i32 = 5;
|
||||
var res: i32 = 0;
|
||||
|
||||
res = recur(n);
|
||||
std.debug.print("再帰関数の合計結果 res = {}\n", .{recur(n)});
|
||||
std.debug.print("再帰関数で求めた和 res = {}\n", .{recur(n)});
|
||||
|
||||
res = forLoopRecur(n);
|
||||
std.debug.print("反復で再帰をシミュレートした合計結果 res = {}\n", .{forLoopRecur(n)});
|
||||
std.debug.print("反復で再帰を再現して求めた和 res = {}\n", .{forLoopRecur(n)});
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
std.debug.print("末尾再帰関数の合計結果 res = {}\n", .{tailRecur(n, 0)});
|
||||
std.debug.print("末尾再帰関数で求めた和 res = {}\n", .{tailRecur(n, 0)});
|
||||
|
||||
res = fib(n);
|
||||
std.debug.print("フィボナッチ数列の第 {} 項は {}\n", .{ n, fib(n) });
|
||||
|
||||
Reference in New Issue
Block a user