主页

go 算法入门(身高的排序:从高到低)

2023-03-30 05:40PM

题目:

输入:names = ["Mary","John","Emma"], heights = [180,165,170]

输出:["Mary","Emma","John"]

解释:Mary 最高,接着是 Emma 和 John

代码如下:

package main
import "fmt"

func sort(name[]string, height[]int) []string{
  for n := 0; n < len(name); n++{
    for h := n+1; h < len(name) && height[n] < height[h]; h++{
        height[n], height[h] = height[h], height[n]
        name[n], name[h] = name[h], name[n]
    }
  }
  return name
}
func main() {
  var name = []string{"Mary", "John", "Emma"}
  var height = []int{180,165,170}
  fmt.Println(sort(name,height)) 
}

结果:

[Mary Emma John]

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论