mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +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:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user