mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-13 02:38:34 +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:
@ -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);
|
||||
|
Reference in New Issue
Block a user