给定一组数,求这组数排列的最大的值
2021-04-26 本文已影响0人
wuhan_goer
题目:
输入 [3, 4, 9, 20, 22, 225]
输出 [9,4,3,225,22,20] // 9432252220
分析:
此题就是冒泡排序的变种,这样看就很简单了。
import (
"fmt"
"strconv"
)
func main() {
a := []int{3, 4, 9, 20, 22, 225}
n := len(a) // 6
// 输出9432252220
for i := 0; i < n; i++ {
for j := 0; j < n-i-1; j++ {
if maxValue(a[j+1], a[j]) {
a[j+1], a[j] = a[j], a[j+1]
}
}
}
fmt.Println(a)
}
func maxValue(a, b int) bool {
tmpA, _ := strconv.Atoi(strconv.Itoa(a) + strconv.Itoa(b))
tmpB, _ := strconv.Atoi(strconv.Itoa(b) + strconv.Itoa(a))
return tmpA > tmpB
}