Sum
Create a program returning the maximum of the sum of horizontal line, sum of the vertical line, and sum of the diagonal line when a 2 dimensional matrix of up to 100x100 is given.
In the case of the following 5x5 matrix, the program should return 29.
[Constraints]
Total of 10 test cases are given.
The matrix size is 100x100 in all cases.
The sum of line does not exceed the maximum integer number.
If there is more than one maximum value, only one is output.
[Input]
The first line of the input file provides the number of the test case. Numbers are provided in next lines.
[Output]
Output the answer to each test case. Each line begins with ‘#x’, and the answer is recorded after a line feed.
Here, x is the number of the test case.
code
#include "stdafx.h"
#include <stdio.h>
#define N 100
int _tmain(int argc, _TCHAR* argv[])
{
int test_case;
int T = 10, t;
static int a[N][N], b[4];
register int i, j;
int max;
freopen("input.txt", "r", stdin);
setbuf(stdout, NULL);
for (test_case = 1; test_case <= T; ++test_case)
{
scanf("%d", &t);
max = 0;
b[2] = 0; b[3] = 0;
for (i = 0; i < N; i++){
b[0] = 0;
for (j = 0; j < N; j++){
scanf("%d", &a[i][j]);
b[0] += a[i][j];
}
if (max < b[0]){
max = b[0];
}
b[2] += a[i][i];
b[3] += a[i][N - 1 - i];
}
if (max < (b[3]>b[2] ? b[3] : b[2])){
max = (b[3]>b[2] ? b[3] : b[2]);
}
for (i = 0; i < N; i++){
b[1] = 0;
for (j = 0; j < N; j++){
b[1] += a[j][i];
}
if (max < b[1]){
max = b[1];
}
}
printf("#%d %d\n", t, max);
}
return 0; //Your program should return 0 on normal termination.
}