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:
Yudong Jin
2024-03-23 02:17:48 +08:00
committed by GitHub
parent fc8473ccfe
commit 3ea91bda99
12 changed files with 69 additions and 69 deletions

View File

@ -90,29 +90,29 @@ fn exp_recur(n: i32) -> i32 {
}
/* 对数阶(循环实现) */
fn logarithmic(mut n: f32) -> i32 {
fn logarithmic(mut n: i32) -> i32 {
let mut count = 0;
while n > 1.0 {
n = n / 2.0;
while n > 1 {
n = n / 2;
count += 1;
}
count
}
/* 对数阶(递归实现) */
fn log_recur(n: f32) -> i32 {
if n <= 1.0 {
fn log_recur(n: i32) -> i32 {
if n <= 1 {
return 0;
}
log_recur(n / 2.0) + 1
log_recur(n / 2) + 1
}
/* 线性对数阶 */
fn linear_log_recur(n: f32) -> i32 {
if n <= 1.0 {
fn linear_log_recur(n: i32) -> i32 {
if n <= 1 {
return 1;
}
let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);
let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);
for _ in 0..n as i32 {
count += 1;
}
@ -157,12 +157,12 @@ fn main() {
count = exp_recur(n);
println!("指数阶(递归实现)的操作数量 = {}", count);
count = logarithmic(n as f32);
count = logarithmic(n);
println!("对数阶(循环实现)的操作数量 = {}", count);
count = log_recur(n as f32);
count = log_recur(n);
println!("对数阶(递归实现)的操作数量 = {}", count);
count = linear_log_recur(n as f32);
count = linear_log_recur(n);
println!("线性对数阶(递归实现)的操作数量 = {}", count);
count = factorial_recur(n);