mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-07 23:57:02 +08:00
Bug fixes and improvements (#1813)
* Sync zh and zh-hant version. * Add the Warp sponsor banner. * Update README with acknowledgments and Warp recommendation Added acknowledgments and a recommendation for the Warp terminal application. * Update README.md * Update links in README.md to use HTTPS * Sync zh and zh-hant versions. * Add special thanks for Warp spnsorship. * Use official warp image link.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
// File: recursion.zig
|
||||
// Created Time: 2023-09-27
|
||||
// Author: QiLOL (pikaqqpika@gmail.com)
|
||||
|
||||
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// 遞迴函式
|
||||
@ -11,7 +11,7 @@ fn recur(n: i32) i32 {
|
||||
return 1;
|
||||
}
|
||||
// 遞:遞迴呼叫
|
||||
var res: i32 = recur(n - 1);
|
||||
const res = recur(n - 1);
|
||||
// 迴:返回結果
|
||||
return n + res;
|
||||
}
|
||||
@ -54,25 +54,35 @@ fn fib(n: i32) i32 {
|
||||
return n - 1;
|
||||
}
|
||||
// 遞迴呼叫 f(n) = f(n-1) + f(n-2)
|
||||
var res: i32 = fib(n - 1) + fib(n - 2);
|
||||
const res: i32 = fib(n - 1) + fib(n - 2);
|
||||
// 返回結果 f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() void {
|
||||
const n: i32 = 5;
|
||||
var res: i32 = 0;
|
||||
|
||||
res = recur(n);
|
||||
std.debug.print("\n遞迴函式的求和結果 res = {}\n", .{recur(n)});
|
||||
std.debug.print("遞迴函式的求和結果 res = {}\n", .{recur(n)});
|
||||
|
||||
res = forLoopRecur(n);
|
||||
std.debug.print("\n使用迭代模擬遞迴的求和結果 res = {}\n", .{forLoopRecur(n)});
|
||||
std.debug.print("使用迭代模擬遞迴的求和結果 res = {}\n", .{forLoopRecur(n)});
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
std.debug.print("\n尾遞迴函式的求和結果 res = {}\n", .{tailRecur(n, 0)});
|
||||
std.debug.print("尾遞迴函式的求和結果 res = {}\n", .{tailRecur(n, 0)});
|
||||
|
||||
res = fib(n);
|
||||
std.debug.print("\n費波那契數列的第 {} 項為 {}\n", .{n, fib(n)});
|
||||
std.debug.print("費波那契數列的第 {} 項為 {}\n", .{ n, fib(n) });
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
run();
|
||||
}
|
||||
|
||||
test "recursion" {
|
||||
run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user