diff --git a/codes/c/chapter_computational_complexity/time_complexity.c b/codes/c/chapter_computational_complexity/time_complexity.c index 5236279d6..d7ffe9021 100644 --- a/codes/c/chapter_computational_complexity/time_complexity.c +++ b/codes/c/chapter_computational_complexity/time_complexity.c @@ -90,7 +90,7 @@ int expRecur(int n) { } /* 对数阶(循环实现) */ -int logarithmic(float n) { +int logarithmic(int n) { int count = 0; while (n > 1) { n = n / 2; @@ -100,14 +100,14 @@ int logarithmic(float n) { } /* 对数阶(递归实现) */ -int logRecur(float n) { +int logRecur(int n) { if (n <= 1) return 0; return logRecur(n / 2) + 1; } /* 线性对数阶 */ -int linearLogRecur(float n) { +int linearLogRecur(int n) { if (n <= 1) return 1; int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); diff --git a/codes/cpp/chapter_computational_complexity/time_complexity.cpp b/codes/cpp/chapter_computational_complexity/time_complexity.cpp index 100498269..5e783edb8 100644 --- a/codes/cpp/chapter_computational_complexity/time_complexity.cpp +++ b/codes/cpp/chapter_computational_complexity/time_complexity.cpp @@ -86,7 +86,7 @@ int expRecur(int n) { } /* 对数阶(循环实现) */ -int logarithmic(float n) { +int logarithmic(int n) { int count = 0; while (n > 1) { n = n / 2; @@ -96,14 +96,14 @@ int logarithmic(float n) { } /* 对数阶(递归实现) */ -int logRecur(float n) { +int logRecur(int n) { if (n <= 1) return 0; return logRecur(n / 2) + 1; } /* 线性对数阶 */ -int linearLogRecur(float n) { +int linearLogRecur(int n) { if (n <= 1) return 1; int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); @@ -153,12 +153,12 @@ int main() { count = expRecur(n); cout << "指数阶(递归实现)的操作数量 = " << count << endl; - count = logarithmic((float)n); + count = logarithmic(n); cout << "对数阶(循环实现)的操作数量 = " << count << endl; - count = logRecur((float)n); + count = logRecur(n); cout << "对数阶(递归实现)的操作数量 = " << count << endl; - count = linearLogRecur((float)n); + count = linearLogRecur(n); cout << "线性对数阶(递归实现)的操作数量 = " << count << endl; count = factorialRecur(n); diff --git a/codes/csharp/chapter_computational_complexity/time_complexity.cs b/codes/csharp/chapter_computational_complexity/time_complexity.cs index 51fd9ab4c..e39c5abf0 100644 --- a/codes/csharp/chapter_computational_complexity/time_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/time_complexity.cs @@ -10,7 +10,7 @@ public class time_complexity { void Algorithm(int n) { int a = 1; // +0(技巧 1) a += n; // +0(技巧 1) - // +n(技巧 2) + // +n(技巧 2) for (int i = 0; i < 5 * n + 1; i++) { Console.WriteLine(0); } @@ -118,7 +118,7 @@ public class time_complexity { } /* 对数阶(循环实现) */ - int Logarithmic(float n) { + int Logarithmic(int n) { int count = 0; while (n > 1) { n /= 2; @@ -128,13 +128,13 @@ public class time_complexity { } /* 对数阶(递归实现) */ - int LogRecur(float n) { + int LogRecur(int n) { if (n <= 1) return 0; return LogRecur(n / 2) + 1; } /* 线性对数阶 */ - int LinearLogRecur(float n) { + int LinearLogRecur(int n) { if (n <= 1) return 1; int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2); for (int i = 0; i < n; i++) { @@ -181,12 +181,12 @@ public class time_complexity { count = ExpRecur(n); Console.WriteLine("指数阶(递归实现)的操作数量 = " + count); - count = Logarithmic((float)n); + count = Logarithmic(n); Console.WriteLine("对数阶(循环实现)的操作数量 = " + count); - count = LogRecur((float)n); + count = LogRecur(n); Console.WriteLine("对数阶(递归实现)的操作数量 = " + count); - count = LinearLogRecur((float)n); + count = LinearLogRecur(n); Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count); count = FactorialRecur(n); diff --git a/codes/dart/chapter_computational_complexity/time_complexity.dart b/codes/dart/chapter_computational_complexity/time_complexity.dart index 0ca83b43f..3bc3afc4b 100644 --- a/codes/dart/chapter_computational_complexity/time_complexity.dart +++ b/codes/dart/chapter_computational_complexity/time_complexity.dart @@ -87,25 +87,25 @@ int expRecur(int n) { } /* 对数阶(循环实现) */ -int logarithmic(num n) { +int logarithmic(int n) { int count = 0; while (n > 1) { - n = n / 2; + n = n ~/ 2; count++; } return count; } /* 对数阶(递归实现) */ -int logRecur(num n) { +int logRecur(int n) { if (n <= 1) return 0; - return logRecur(n / 2) + 1; + return logRecur(n ~/ 2) + 1; } /* 线性对数阶 */ -int linearLogRecur(num n) { +int linearLogRecur(int n) { if (n <= 1) return 1; - int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2); for (var i = 0; i < n; i++) { count++; } diff --git a/codes/go/chapter_computational_complexity/time_complexity.go b/codes/go/chapter_computational_complexity/time_complexity.go index 9d001de11..3cbe31af2 100644 --- a/codes/go/chapter_computational_complexity/time_complexity.go +++ b/codes/go/chapter_computational_complexity/time_complexity.go @@ -87,7 +87,7 @@ func expRecur(n int) int { } /* 对数阶(循环实现)*/ -func logarithmic(n float64) int { +func logarithmic(n int) int { count := 0 for n > 1 { n = n / 2 @@ -97,7 +97,7 @@ func logarithmic(n float64) int { } /* 对数阶(递归实现)*/ -func logRecur(n float64) int { +func logRecur(n int) int { if n <= 1 { return 0 } @@ -105,12 +105,12 @@ func logRecur(n float64) int { } /* 线性对数阶 */ -func linearLogRecur(n float64) int { +func linearLogRecur(n int) int { if n <= 1 { return 1 } count := linearLogRecur(n/2) + linearLogRecur(n/2) - for i := 0.0; i < n; i++ { + for i := 0; i < n; i++ { count++ } return count diff --git a/codes/go/chapter_computational_complexity/time_complexity_test.go b/codes/go/chapter_computational_complexity/time_complexity_test.go index adfbdd8d3..819ce7095 100644 --- a/codes/go/chapter_computational_complexity/time_complexity_test.go +++ b/codes/go/chapter_computational_complexity/time_complexity_test.go @@ -35,12 +35,12 @@ func TestTimeComplexity(t *testing.T) { count = expRecur(n) fmt.Println("指数阶(递归实现)的操作数量 =", count) - count = logarithmic(float64(n)) + count = logarithmic(n) fmt.Println("对数阶(循环实现)的操作数量 =", count) - count = logRecur(float64(n)) + count = logRecur(n) fmt.Println("对数阶(递归实现)的操作数量 =", count) - count = linearLogRecur(float64(n)) + count = linearLogRecur(n) fmt.Println("线性对数阶(递归实现)的操作数量 =", count) count = factorialRecur(n) diff --git a/codes/java/chapter_computational_complexity/time_complexity.java b/codes/java/chapter_computational_complexity/time_complexity.java index 47698b949..04d6b199a 100644 --- a/codes/java/chapter_computational_complexity/time_complexity.java +++ b/codes/java/chapter_computational_complexity/time_complexity.java @@ -87,7 +87,7 @@ public class time_complexity { } /* 对数阶(循环实现) */ - static int logarithmic(float n) { + static int logarithmic(int n) { int count = 0; while (n > 1) { n = n / 2; @@ -97,14 +97,14 @@ public class time_complexity { } /* 对数阶(递归实现) */ - static int logRecur(float n) { + static int logRecur(int n) { if (n <= 1) return 0; return logRecur(n / 2) + 1; } /* 线性对数阶 */ - static int linearLogRecur(float n) { + static int linearLogRecur(int n) { if (n <= 1) return 1; int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); @@ -153,12 +153,12 @@ public class time_complexity { count = expRecur(n); System.out.println("指数阶(递归实现)的操作数量 = " + count); - count = logarithmic((float) n); + count = logarithmic(n); System.out.println("对数阶(循环实现)的操作数量 = " + count); - count = logRecur((float) n); + count = logRecur(n); System.out.println("对数阶(递归实现)的操作数量 = " + count); - count = linearLogRecur((float) n); + count = linearLogRecur(n); System.out.println("线性对数阶(递归实现)的操作数量 = " + count); count = factorialRecur(n); diff --git a/codes/kotlin/chapter_computational_complexity/time_complexity.kt b/codes/kotlin/chapter_computational_complexity/time_complexity.kt index 4addb982f..04fe31127 100644 --- a/codes/kotlin/chapter_computational_complexity/time_complexity.kt +++ b/codes/kotlin/chapter_computational_complexity/time_complexity.kt @@ -87,7 +87,7 @@ fun expRecur(n: Int): Int { } /* 对数阶(循环实现) */ -fun logarithmic(n: Float): Int { +fun logarithmic(n: Int): Int { var n1 = n var count = 0 while (n1 > 1) { @@ -98,14 +98,14 @@ fun logarithmic(n: Float): Int { } /* 对数阶(递归实现) */ -fun logRecur(n: Float): Int { +fun logRecur(n: Int): Int { if (n <= 1) return 0 return logRecur(n / 2) + 1 } /* 线性对数阶 */ -fun linearLogRecur(n: Float): Int { +fun linearLogRecur(n: Int): Int { if (n <= 1) return 1 var count = linearLogRecur(n / 2) + linearLogRecur(n / 2) @@ -153,12 +153,12 @@ fun main() { count = expRecur(n) println("指数阶(递归实现)的操作数量 = $count") - count = logarithmic(n.toFloat()) + count = logarithmic(n) println("对数阶(循环实现)的操作数量 = $count") - count = logRecur(n.toFloat()) + count = logRecur(n) println("对数阶(递归实现)的操作数量 = $count") - count = linearLogRecur(n.toFloat()) + count = linearLogRecur(n) println("线性对数阶(递归实现)的操作数量 = $count") count = factorialRecur(n) diff --git a/codes/python/chapter_computational_complexity/time_complexity.py b/codes/python/chapter_computational_complexity/time_complexity.py index 045062731..ac404c831 100644 --- a/codes/python/chapter_computational_complexity/time_complexity.py +++ b/codes/python/chapter_computational_complexity/time_complexity.py @@ -77,7 +77,7 @@ def exp_recur(n: int) -> int: return exp_recur(n - 1) + exp_recur(n - 1) + 1 -def logarithmic(n: float) -> int: +def logarithmic(n: int) -> int: """对数阶(循环实现)""" count = 0 while n > 1: @@ -86,14 +86,14 @@ def logarithmic(n: float) -> int: return count -def log_recur(n: float) -> int: +def log_recur(n: int) -> int: """对数阶(递归实现)""" if n <= 1: return 0 return log_recur(n / 2) + 1 -def linear_log_recur(n: float) -> int: +def linear_log_recur(n: int) -> int: """线性对数阶""" if n <= 1: return 1 diff --git a/codes/rust/chapter_computational_complexity/time_complexity.rs b/codes/rust/chapter_computational_complexity/time_complexity.rs index a0951bd53..70d451068 100644 --- a/codes/rust/chapter_computational_complexity/time_complexity.rs +++ b/codes/rust/chapter_computational_complexity/time_complexity.rs @@ -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); diff --git a/codes/swift/chapter_computational_complexity/time_complexity.swift b/codes/swift/chapter_computational_complexity/time_complexity.swift index e50e9fa3d..4356e4baa 100644 --- a/codes/swift/chapter_computational_complexity/time_complexity.swift +++ b/codes/swift/chapter_computational_complexity/time_complexity.swift @@ -88,7 +88,7 @@ func expRecur(n: Int) -> Int { } /* 对数阶(循环实现) */ -func logarithmic(n: Double) -> Int { +func logarithmic(n: Int) -> Int { var count = 0 var n = n while n > 1 { @@ -99,7 +99,7 @@ func logarithmic(n: Double) -> Int { } /* 对数阶(递归实现) */ -func logRecur(n: Double) -> Int { +func logRecur(n: Int) -> Int { if n <= 1 { return 0 } @@ -107,7 +107,7 @@ func logRecur(n: Double) -> Int { } /* 线性对数阶 */ -func linearLogRecur(n: Double) -> Int { +func linearLogRecur(n: Int) -> Int { if n <= 1 { return 1 } @@ -158,12 +158,12 @@ enum TimeComplexity { count = expRecur(n: n) print("指数阶(递归实现)的操作数量 = \(count)") - count = logarithmic(n: Double(n)) + count = logarithmic(n: n) print("对数阶(循环实现)的操作数量 = \(count)") - count = logRecur(n: Double(n)) + count = logRecur(n: n) print("对数阶(递归实现)的操作数量 = \(count)") - count = linearLogRecur(n: Double(n)) + count = linearLogRecur(n: n) print("线性对数阶(递归实现)的操作数量 = \(count)") count = factorialRecur(n: n) diff --git a/codes/zig/chapter_computational_complexity/time_complexity.zig b/codes/zig/chapter_computational_complexity/time_complexity.zig index 3437c7e1c..3d2ea188b 100644 --- a/codes/zig/chapter_computational_complexity/time_complexity.zig +++ b/codes/zig/chapter_computational_complexity/time_complexity.zig @@ -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);