mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
fix: Use int instead of float for the example code of log time complexity (#1164)
* Use int instead of float for the example code of log time complexity * Bug fixes * Bug fixes
This commit is contained in:
@ -95,7 +95,7 @@ fn expRecur(n: i32) i32 {
|
||||
}
|
||||
|
||||
// 对数阶(循环实现)
|
||||
fn logarithmic(n: f32) i32 {
|
||||
fn logarithmic(n: i32) i32 {
|
||||
var count: i32 = 0;
|
||||
var n_var = n;
|
||||
while (n_var > 1)
|
||||
@ -107,16 +107,16 @@ fn logarithmic(n: f32) i32 {
|
||||
}
|
||||
|
||||
// 对数阶(递归实现)
|
||||
fn logRecur(n: f32) i32 {
|
||||
fn logRecur(n: i32) i32 {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
// 线性对数阶
|
||||
fn linearLogRecur(n: f32) i32 {
|
||||
fn linearLogRecur(n: i32) i32 {
|
||||
if (n <= 1) return 1;
|
||||
var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
var i: f32 = 0;
|
||||
var i: i32 = 0;
|
||||
while (i < n) : (i += 1) {
|
||||
count += 1;
|
||||
}
|
||||
@ -163,12 +163,12 @@ pub fn main() !void {
|
||||
count = expRecur(n);
|
||||
std.debug.print("指数阶(递归实现)的操作数量 = {}\n", .{count});
|
||||
|
||||
count = logarithmic(@as(f32, n));
|
||||
count = logarithmic(n);
|
||||
std.debug.print("对数阶(循环实现)的操作数量 = {}\n", .{count});
|
||||
count = logRecur(@as(f32, n));
|
||||
count = logRecur(n);
|
||||
std.debug.print("对数阶(递归实现)的操作数量 = {}\n", .{count});
|
||||
|
||||
count = linearLogRecur(@as(f32, n));
|
||||
count = linearLogRecur(n);
|
||||
std.debug.print("线性对数阶(递归实现)的操作数量 = {}\n", .{count});
|
||||
|
||||
count = factorialRecur(n);
|
||||
|
||||
Reference in New Issue
Block a user