题解 | #Problem B#

Problem B

https://www.nowcoder.com/practice/bcd4ec5971054997a1dc282067aa1d8b

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <cmath>
using namespace std;

int arr[11][11];
int res[50];

bool compare(int o1, int o2) {
    return o1 > o2;
}

int main() {
    int m;
    while (scanf("%d", &m) != EOF) { // 注意 while 处理多个 case
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                scanf("%d", &arr[i][j]);
            }
        }
        int index = 0; //存放数组的下标

        for (int i = 0; i < m; i++) { //每行之和
            int sum = 0;
            for (int j = 0; j < m; j++) {
                sum += arr[i][j];
            }
            res[index++] = sum;
        }

        for (int i = 0; i < m; i++) { //每列之和
            int sum = 0;
            for (int j = 0; j < m; j++) {
                sum += arr[j][i];
            }
            res[index++] = sum;
        }

        int sum = 0;
        for (int i = 0; i < m; i++) { //主对角线之和
            sum += arr[i][i];

        }
        res[index++] = sum;

        sum = 0;
        for (int i = 0; i < m; i++) { //副对角线之和
            sum += arr[i][m - 1 - i];

        }
        res[index++] = sum;

        sort(res, res + index, compare);

        for (int i = 0; i < index; i++) {
            printf("%d ", res[i]);
        }

        printf("\n");

    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务