mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 09:26:28 +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:
parent
fc8473ccfe
commit
3ea91bda99
12 changed files with 69 additions and 69 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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++;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue