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
pull/1170/head
Yudong Jin 8 months ago committed by GitHub
parent fc8473ccfe
commit 3ea91bda99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -90,7 +90,7 @@ int expRecur(int n) {
} }
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
int logarithmic(float n) { int logarithmic(int n) {
int count = 0; int count = 0;
while (n > 1) { while (n > 1) {
n = n / 2; n = n / 2;
@ -100,14 +100,14 @@ int logarithmic(float n) {
} }
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
int logRecur(float n) { int logRecur(int n) {
if (n <= 1) if (n <= 1)
return 0; return 0;
return logRecur(n / 2) + 1; return logRecur(n / 2) + 1;
} }
/* 线性对数阶 */ /* 线性对数阶 */
int linearLogRecur(float n) { int linearLogRecur(int n) {
if (n <= 1) if (n <= 1)
return 1; return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);

@ -86,7 +86,7 @@ int expRecur(int n) {
} }
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
int logarithmic(float n) { int logarithmic(int n) {
int count = 0; int count = 0;
while (n > 1) { while (n > 1) {
n = n / 2; n = n / 2;
@ -96,14 +96,14 @@ int logarithmic(float n) {
} }
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
int logRecur(float n) { int logRecur(int n) {
if (n <= 1) if (n <= 1)
return 0; return 0;
return logRecur(n / 2) + 1; return logRecur(n / 2) + 1;
} }
/* 线性对数阶 */ /* 线性对数阶 */
int linearLogRecur(float n) { int linearLogRecur(int n) {
if (n <= 1) if (n <= 1)
return 1; return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
@ -153,12 +153,12 @@ int main() {
count = expRecur(n); count = expRecur(n);
cout << "指数阶(递归实现)的操作数量 = " << count << endl; cout << "指数阶(递归实现)的操作数量 = " << count << endl;
count = logarithmic((float)n); count = logarithmic(n);
cout << "对数阶(循环实现)的操作数量 = " << count << endl; cout << "对数阶(循环实现)的操作数量 = " << count << endl;
count = logRecur((float)n); count = logRecur(n);
cout << "对数阶(递归实现)的操作数量 = " << count << endl; cout << "对数阶(递归实现)的操作数量 = " << count << endl;
count = linearLogRecur((float)n); count = linearLogRecur(n);
cout << "线性对数阶(递归实现)的操作数量 = " << count << endl; cout << "线性对数阶(递归实现)的操作数量 = " << count << endl;
count = factorialRecur(n); count = factorialRecur(n);

@ -118,7 +118,7 @@ public class time_complexity {
} }
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
int Logarithmic(float n) { int Logarithmic(int n) {
int count = 0; int count = 0;
while (n > 1) { while (n > 1) {
n /= 2; n /= 2;
@ -128,13 +128,13 @@ public class time_complexity {
} }
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
int LogRecur(float n) { int LogRecur(int n) {
if (n <= 1) return 0; if (n <= 1) return 0;
return LogRecur(n / 2) + 1; return LogRecur(n / 2) + 1;
} }
/* 线性对数阶 */ /* 线性对数阶 */
int LinearLogRecur(float n) { int LinearLogRecur(int n) {
if (n <= 1) return 1; if (n <= 1) return 1;
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2); int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@ -181,12 +181,12 @@ public class time_complexity {
count = ExpRecur(n); count = ExpRecur(n);
Console.WriteLine("指数阶(递归实现)的操作数量 = " + count); Console.WriteLine("指数阶(递归实现)的操作数量 = " + count);
count = Logarithmic((float)n); count = Logarithmic(n);
Console.WriteLine("对数阶(循环实现)的操作数量 = " + count); Console.WriteLine("对数阶(循环实现)的操作数量 = " + count);
count = LogRecur((float)n); count = LogRecur(n);
Console.WriteLine("对数阶(递归实现)的操作数量 = " + count); Console.WriteLine("对数阶(递归实现)的操作数量 = " + count);
count = LinearLogRecur((float)n); count = LinearLogRecur(n);
Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count); Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count);
count = FactorialRecur(n); count = FactorialRecur(n);

@ -87,25 +87,25 @@ int expRecur(int n) {
} }
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
int logarithmic(num n) { int logarithmic(int n) {
int count = 0; int count = 0;
while (n > 1) { while (n > 1) {
n = n / 2; n = n ~/ 2;
count++; count++;
} }
return count; return count;
} }
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
int logRecur(num n) { int logRecur(int n) {
if (n <= 1) return 0; 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; 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++) { for (var i = 0; i < n; i++) {
count++; count++;
} }

@ -87,7 +87,7 @@ func expRecur(n int) int {
} }
/* 对数阶(循环实现)*/ /* 对数阶(循环实现)*/
func logarithmic(n float64) int { func logarithmic(n int) int {
count := 0 count := 0
for n > 1 { for n > 1 {
n = n / 2 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 { if n <= 1 {
return 0 return 0
} }
@ -105,12 +105,12 @@ func logRecur(n float64) int {
} }
/* 线性对数阶 */ /* 线性对数阶 */
func linearLogRecur(n float64) int { func linearLogRecur(n int) int {
if n <= 1 { if n <= 1 {
return 1 return 1
} }
count := linearLogRecur(n/2) + linearLogRecur(n/2) count := linearLogRecur(n/2) + linearLogRecur(n/2)
for i := 0.0; i < n; i++ { for i := 0; i < n; i++ {
count++ count++
} }
return count return count

@ -35,12 +35,12 @@ func TestTimeComplexity(t *testing.T) {
count = expRecur(n) count = expRecur(n)
fmt.Println("指数阶(递归实现)的操作数量 =", count) fmt.Println("指数阶(递归实现)的操作数量 =", count)
count = logarithmic(float64(n)) count = logarithmic(n)
fmt.Println("对数阶(循环实现)的操作数量 =", count) fmt.Println("对数阶(循环实现)的操作数量 =", count)
count = logRecur(float64(n)) count = logRecur(n)
fmt.Println("对数阶(递归实现)的操作数量 =", count) fmt.Println("对数阶(递归实现)的操作数量 =", count)
count = linearLogRecur(float64(n)) count = linearLogRecur(n)
fmt.Println("线性对数阶(递归实现)的操作数量 =", count) fmt.Println("线性对数阶(递归实现)的操作数量 =", count)
count = factorialRecur(n) count = factorialRecur(n)

@ -87,7 +87,7 @@ public class time_complexity {
} }
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
static int logarithmic(float n) { static int logarithmic(int n) {
int count = 0; int count = 0;
while (n > 1) { while (n > 1) {
n = n / 2; n = n / 2;
@ -97,14 +97,14 @@ public class time_complexity {
} }
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
static int logRecur(float n) { static int logRecur(int n) {
if (n <= 1) if (n <= 1)
return 0; return 0;
return logRecur(n / 2) + 1; return logRecur(n / 2) + 1;
} }
/* 线性对数阶 */ /* 线性对数阶 */
static int linearLogRecur(float n) { static int linearLogRecur(int n) {
if (n <= 1) if (n <= 1)
return 1; return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
@ -153,12 +153,12 @@ public class time_complexity {
count = expRecur(n); count = expRecur(n);
System.out.println("指数阶(递归实现)的操作数量 = " + count); System.out.println("指数阶(递归实现)的操作数量 = " + count);
count = logarithmic((float) n); count = logarithmic(n);
System.out.println("对数阶(循环实现)的操作数量 = " + count); System.out.println("对数阶(循环实现)的操作数量 = " + count);
count = logRecur((float) n); count = logRecur(n);
System.out.println("对数阶(递归实现)的操作数量 = " + count); System.out.println("对数阶(递归实现)的操作数量 = " + count);
count = linearLogRecur((float) n); count = linearLogRecur(n);
System.out.println("线性对数阶(递归实现)的操作数量 = " + count); System.out.println("线性对数阶(递归实现)的操作数量 = " + count);
count = factorialRecur(n); count = factorialRecur(n);

@ -87,7 +87,7 @@ fun expRecur(n: Int): Int {
} }
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
fun logarithmic(n: Float): Int { fun logarithmic(n: Int): Int {
var n1 = n var n1 = n
var count = 0 var count = 0
while (n1 > 1) { 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) if (n <= 1)
return 0 return 0
return logRecur(n / 2) + 1 return logRecur(n / 2) + 1
} }
/* 线性对数阶 */ /* 线性对数阶 */
fun linearLogRecur(n: Float): Int { fun linearLogRecur(n: Int): Int {
if (n <= 1) if (n <= 1)
return 1 return 1
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2) var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
@ -153,12 +153,12 @@ fun main() {
count = expRecur(n) count = expRecur(n)
println("指数阶(递归实现)的操作数量 = $count") println("指数阶(递归实现)的操作数量 = $count")
count = logarithmic(n.toFloat()) count = logarithmic(n)
println("对数阶(循环实现)的操作数量 = $count") println("对数阶(循环实现)的操作数量 = $count")
count = logRecur(n.toFloat()) count = logRecur(n)
println("对数阶(递归实现)的操作数量 = $count") println("对数阶(递归实现)的操作数量 = $count")
count = linearLogRecur(n.toFloat()) count = linearLogRecur(n)
println("线性对数阶(递归实现)的操作数量 = $count") println("线性对数阶(递归实现)的操作数量 = $count")
count = factorialRecur(n) count = factorialRecur(n)

@ -77,7 +77,7 @@ def exp_recur(n: int) -> int:
return exp_recur(n - 1) + exp_recur(n - 1) + 1 return exp_recur(n - 1) + exp_recur(n - 1) + 1
def logarithmic(n: float) -> int: def logarithmic(n: int) -> int:
"""对数阶(循环实现)""" """对数阶(循环实现)"""
count = 0 count = 0
while n > 1: while n > 1:
@ -86,14 +86,14 @@ def logarithmic(n: float) -> int:
return count return count
def log_recur(n: float) -> int: def log_recur(n: int) -> int:
"""对数阶(递归实现)""" """对数阶(递归实现)"""
if n <= 1: if n <= 1:
return 0 return 0
return log_recur(n / 2) + 1 return log_recur(n / 2) + 1
def linear_log_recur(n: float) -> int: def linear_log_recur(n: int) -> int:
"""线性对数阶""" """线性对数阶"""
if n <= 1: if n <= 1:
return 1 return 1

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

@ -88,7 +88,7 @@ func expRecur(n: Int) -> Int {
} }
/* */ /* */
func logarithmic(n: Double) -> Int { func logarithmic(n: Int) -> Int {
var count = 0 var count = 0
var n = n var n = n
while n > 1 { 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 { if n <= 1 {
return 0 return 0
} }
@ -107,7 +107,7 @@ func logRecur(n: Double) -> Int {
} }
/* 线 */ /* 线 */
func linearLogRecur(n: Double) -> Int { func linearLogRecur(n: Int) -> Int {
if n <= 1 { if n <= 1 {
return 1 return 1
} }
@ -158,12 +158,12 @@ enum TimeComplexity {
count = expRecur(n: n) count = expRecur(n: n)
print("指数阶(递归实现)的操作数量 = \(count)") print("指数阶(递归实现)的操作数量 = \(count)")
count = logarithmic(n: Double(n)) count = logarithmic(n: n)
print("对数阶(循环实现)的操作数量 = \(count)") print("对数阶(循环实现)的操作数量 = \(count)")
count = logRecur(n: Double(n)) count = logRecur(n: n)
print("对数阶(递归实现)的操作数量 = \(count)") print("对数阶(递归实现)的操作数量 = \(count)")
count = linearLogRecur(n: Double(n)) count = linearLogRecur(n: n)
print("线性对数阶(递归实现)的操作数量 = \(count)") print("线性对数阶(递归实现)的操作数量 = \(count)")
count = factorialRecur(n: n) count = factorialRecur(n: n)

@ -95,7 +95,7 @@ fn expRecur(n: i32) i32 {
} }
// //
fn logarithmic(n: f32) i32 { fn logarithmic(n: i32) i32 {
var count: i32 = 0; var count: i32 = 0;
var n_var = n; var n_var = n;
while (n_var > 1) 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; if (n <= 1) return 0;
return logRecur(n / 2) + 1; return logRecur(n / 2) + 1;
} }
// 线 // 线
fn linearLogRecur(n: f32) i32 { fn linearLogRecur(n: i32) i32 {
if (n <= 1) return 1; if (n <= 1) return 1;
var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2); var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);
var i: f32 = 0; var i: i32 = 0;
while (i < n) : (i += 1) { while (i < n) : (i += 1) {
count += 1; count += 1;
} }
@ -163,12 +163,12 @@ pub fn main() !void {
count = expRecur(n); count = expRecur(n);
std.debug.print("指数阶(递归实现)的操作数量 = {}\n", .{count}); std.debug.print("指数阶(递归实现)的操作数量 = {}\n", .{count});
count = logarithmic(@as(f32, n)); count = logarithmic(n);
std.debug.print("对数阶(循环实现)的操作数量 = {}\n", .{count}); std.debug.print("对数阶(循环实现)的操作数量 = {}\n", .{count});
count = logRecur(@as(f32, n)); count = logRecur(n);
std.debug.print("对数阶(递归实现)的操作数量 = {}\n", .{count}); std.debug.print("对数阶(递归实现)的操作数量 = {}\n", .{count});
count = linearLogRecur(@as(f32, n)); count = linearLogRecur(n);
std.debug.print("线性对数阶(递归实现)的操作数量 = {}\n", .{count}); std.debug.print("线性对数阶(递归实现)的操作数量 = {}\n", .{count});
count = factorialRecur(n); count = factorialRecur(n);

Loading…
Cancel
Save