A2. Good Matrix Elements

The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:

Elements of the main diagonal.
Elements of the secondary diagonal.
Elements of the “middle” row — the row which has exactly rows above it and the same number of rows below it.
Elements of the “middle” column — the column that has exactly columns to the left of it and the same number of columns to the right of it.
The figure shows a 5 × 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.

Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≤ aij ≤ 100) separated by single spaces — the elements of the given matrix.

The input limitations for getting 30 points are:

1 ≤ n ≤ 5
The input limitations for getting 100 points are:

1 ≤ n ≤ 101
Output
Print a single integer — the sum of good matrix elements.

Examples
inputCopy
3
1 2 3
4 5 6
7 8 9
outputCopy
45
inputCopy
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
outputCopy
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.

#include <iostream>
#include <algorithm>
using namespace std;

int a[105][105];

int main()
{
    int n;
    while(cin >> n)
    {
        int sum = 0;
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                cin >> a[i][j];
        for(int i = 0; i < n; ++i)
            sum += a[n / 2][i];
        for(int i = 0; i < n; ++i)
            sum += a[i][n / 2];
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                if(i == j || max(i, j) + min(i, j) == n - 1)
                    sum += a[i][j];///注意:最終間的數在這裏僅被加了一次
        cout << sum - 2 * a[n / 2][n / 2] << '\n';
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章