题解 | #矩阵最大值#
矩阵最大值
https://www.nowcoder.com/practice/9c550b6fe85d48bcad5a6025c6dc447d
#include<limits.h> int型无穷小表示INT_MIN
注意!每次判断完本层最大值以及累和之后,要记得重置
#include <iostream> using namespace std; #include<limits.h> #define N 101 #define M 101 int main(){ int m,n; while (cin >>m>>n){ int a[M][N]; int i,j; int max = INT_MIN,max_i,max_j; int sum = 0; for (i=0;i<m;i++){ sum = 0;//重置sum max = INT_MIN;//重置max for (j=0;j<n;j++){ cin >>a[i][j]; //输入 if (a[i][j] > max){ //记录本行最大值 以及下标 max = a[i][j]; max_i = i; max_j = j; } sum += a[i][j];//本行总和 } //本行最大值的元素值 = 本行总和 a[max_i][max_j] = sum; } for (i=0;i<m;i++){ for (j=0;j<n;j++){ cout <<a[i][j]<<" "; } cout <<endl; } } }