题解 | #矩阵最大值#

矩阵最大值

https://www.nowcoder.com/practice/9c550b6fe85d48bcad5a6025c6dc447d

#include<stdio.h>

const int M = 1000;
const int N = 1000;

int a[M][N];

int main() {
    int m, n;
    while(scanf("%d%d", &m, &n) != EOF) {
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                scanf("%d", &a[i][j]);
        
        for(int i = 0; i < m; i++) {
            int max = 0, sum = 0;
            for(int j = 0; j < n; j++) {
                sum += a[i][j];
                if(a[i][j] > a[i][max]) max = j;
            }
            a[i][max] = sum;
        }

        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(j == n - 1) printf("%d\n", a[i][j]);
                else printf("%d ", a[i][j]);
    }
    return 0;
}

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务