Golang 编写一个求int slice([] int)的最大值最小值的函数

Jackey Golang 8,652 次浏览 2条评论
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func max(l []int) (max int) {
  6. max = l[0]
  7. for _, v := range l {
  8. if v > max {
  9. max = v
  10. }
  11. }
  12. return
  13. }
  14.  
  15. func min(l []int) (min int) {
  16. min = l[0]
  17. for _, v := range l {
  18. if v < min {
  19. min = v
  20. }
  21. }
  22. return
  23. }
  24.  
  25. func main() {
  26. l := []int{1, 3, 4}
  27. the_max := max(l)
  28. the_min := min(l)
  29. fmt.Printf("The max is %d\n", the_max)
  30. fmt.Printf("The min is %d\n", the_min)
  31. }

2 条评论

  1. 夏日博客 2019年12月13日 下午1:18 回复

    主域名这是不用了吗。

    • PHP二次开发 2020年1月8日 上午11:22 回复

      暂时不用了哈!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Go