Question E: Problem B Codeup ContestID: 100000581

Topic links : http://codeup.cn/problem.php?cid=100000581&pid=4

Title Description
writing a program that, for an m rows and m columns (1 <m <10) is square, each seeking the row, and each column of elements of the main diagonal and, finally sequentially in descending order output.

Input
common set of data, the first input acts a positive integer, m represents, the next line m, m represents an integer of matrix elements of each row.

Output
descending integer line arrangement, each integer followed by a space, the last wrap.

Sample input
. 4
15 -2. 6. 8
31 is 18 is 71 is 24
-3 13 is 27 -9
. 17 21 is 38 is 69

Sample Output
159 1,451,441,358,160,443,228 27

Code

#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main() {
	int m, a[10][10];
	while(scanf("%d", &m) != EOF){
		int b[30] = {0};
		for(int i = 0; i < m; i++)
		for(int j = 0; j < m; j++) 
			scanf("%d", &a[i][j]);
		for(int i = 0; i < m; i++)      //行之和
			for(int j = 0; j < m; j++) 
				b[i] += a[i][j];
		for(int j = 0; j < m; j++)		//列之和
			for(int i = 0; i < m; i++) 
				b[m + j] += a[i][j];
		for(int i = 0, j = 0; i < m && j < m; i++, j++)		//主对角线之和
			b[2 * m] += a[i][j];
		for(int i = 0, j = m - 1; i < m && j >= 0; i++, j--)	//副对角线之和
			b[2 * m + 1] += a[i][j];
		sort(b, b + 2 * m + 2, cmp);
		for(int i = 0; i < 2 * m + 2; i++)
			printf("%d ", b[i]);
		printf("\n");
	}
	return 0;
}

[Note]: OJ pass for multiple sets of data input (although not say in the title), otherwise wrong answer 50.

Published 99 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/Rhao999/article/details/104066154