This commit is contained in:
krahets
2024-04-13 21:17:44 +08:00
parent 9332a91e26
commit 6afa70e7bc
55 changed files with 334 additions and 182 deletions

View File

@ -1431,7 +1431,6 @@ $$
/* 線性階 */
fun linear(n: Int): Int {
var count = 0
// 迴圈次數與陣列長度成正比
for (i in 0..<n)
count++
return count
@ -2175,7 +2174,9 @@ $$
for (j in 0..<i) {
if (nums[j] > nums[j + 1]) {
// 交換 nums[j] 與 nums[j + 1]
nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }
val temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
count += 3 // 元素交換包含 3 個單元操作
}
}
@ -2452,8 +2453,8 @@ $$
/* 指數階(迴圈實現) */
fun exponential(n: Int): Int {
var count = 0
// 細胞每輪一分為二,形成數列 1, 2, 4, 8, ..., 2^(n-1)
var base = 1
// 細胞每輪一分為二,形成數列 1, 2, 4, 8, ..., 2^(n-1)
for (i in 0..<n) {
for (j in 0..<base) {
count++
@ -3859,12 +3860,11 @@ $$
for (i in 0..<n) {
nums[i] = i + 1
}
val mutableList = nums.toMutableList()
// 隨機打亂陣列元素
mutableList.shuffle()
nums.shuffle()
val res = arrayOfNulls<Int>(n)
for (i in 0..<n) {
res[i] = mutableList[i]
res[i] = nums[i]
}
return res
}