package main import "fmt" func main() { arr := [5][5]int{ {1, 0, 3, 4, 0}, {0, 0, 3, 4, 0}, {0, 0, 3, 2, 0}, {0, 0, 9, 2, 0}, {0, 0, 3, 0, 0}, } r:=[5]int{} for i := 0; i < len(arr); i++ { sumLine := 0 for j := 0; j < len(arr[i]); j++ { sumLine+=arr[i][j] }//for j r[i]=sumLine }//fori fmt.Printf("%v ",r) //假设第一个元素是最大值,下标为0 maxVal := r[0] maxIndex := 0 for i := 1; i < len(r); i++ { //从第二个 元素开始循环比较,如果发现有更大的,则交换 if maxVal < r[i] { maxVal = r[i] maxIndex = i } } fmt.Printf("maxVal=%v, maxIndex=%v", maxVal, maxIndex) }
Golang求矩阵每行最大值,获取int数组里的最大值和下标
阅读:3455 输入:2021-06-22 15:23:12