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:
Yudong Jin
2024-03-23 02:17:48 +08:00
committed by GitHub
parent fc8473ccfe
commit 3ea91bda99
12 changed files with 69 additions and 69 deletions

View File

@ -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)