4 Sum

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.
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值