Xtreme8.0

Xtreme8.0 - Magic Square

题目连接:

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/magic-square

Description

Johnny designed a magic square (square of numbers with the same sum for all rows, columns and diagonals i.e. both the main diagonal - meaning the diagonal that leads from the top-left corner towards bottom-right corner - and the antidiagonal - meaning the diagonal that leads from top-right corner towards bottom-left corner). Write a program to test it.

Input

Input begins with a single integer T, 1 <= T <= 100, which denotes number of test cases.

Each test case begins with a line, which contains 2 space-separated integers D and S. D represents the number of words in a dictionary, and S represents the number of potential dictionary strings to be checked. Note that 1 <= D, S <= 100.

Next follows D lines, each containing a word in the dictionary.

The remaining S lines in the test case each contain a potential dictionary string.

Notes: The words in the dictionary and the potential dictionary strings will consist of only lower-case letters. The lengths of these strings are greater than or equal to one character and less than or equal to 40,000 characters.

Output

Write a program that will check if the given square is magic (i.e. has the same sum for all rows, columns and diagonals).

Sample Input

3
8 1 6
3 5 7
4 9 2

Sample Output

0

Hint

The input square looks as follows: IMAGE 1

The square has 4 rows (labeled from 1 to 4 in orange) and 4 columns (labeled from -1 to -4 in green) as depicted in the image above. The main diagonal and antidiagonal of the square are highlighted in red and blue respectively.

The main diagonal has sum = 16 + 10 + 7 +1 = 34.
The antidiagonal has sum = 13 + 11 + 9 + 4 = 37. This is different to the sum of the main diagonal so value 0 corresponding to the antidiagonal should be reported.
Row 1 has sum = 16 + 3 + 2 + 13 = 34.
Row 2 has sum = 5 + 10 + 11 + 8 = 34.
Row 3 has sum = 6 + 9 + 7 + 12 = 34.
Row 4 has sum = 4 + 15 + 14 + 1 = 34.
Column -1 has sum = 16 + 5 + 6 + 4 = 31. This is different to the sum of the main diagonal so value -1 should be reported.
Column -2 has sum = 3 + 10 + 9 + 15 = 37. This is different to the sum of the main diagonal so value -2 should be reported.
Column -3 has sum = 2 + 11 + 7 + 14 = 34.
Column -4 has sum = 13 + 8 + 12 + 1 = 34.
Based on the above, there are 3 lines that do not sum up to the sum of the elements of the main diagonal. Since they should be sorted in incremental order, the output should be:
3
-2
-1
0

题意

给你一个正方形,问你有哪些行和哪些列和反对角线,是否和正对角线相同。

然后输出答案。

题解

直接暴力去搞就好了,每一行,每一列都去判断就好了。

代码

 #include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int a[maxn][maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            scanf("%d",&a[i][j]);
    int sum = 0;
    for(int i=1;i<=n;i++)
        sum+=a[i][i];
    vector<int>ans;
    int sum2=0;
    for(int i=1;i<=n;i++)
        sum2+=a[i][n-i+1];
    if(sum2!=sum)ans.push_back(0);
    for(int i=1;i<=n;i++){
        sum2=0;
        for(int j=1;j<=n;j++)
            sum2+=a[i][j];
        if(sum2!=sum)ans.push_back(i);
    }
    for(int i=1;i<=n;i++){
        sum2=0;
        for(int j=1;j<=n;j++)
            sum2+=a[j][i];
        if(sum2!=sum)ans.push_back(-i);
    }
    sort(ans.begin(),ans.end());
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++)
        cout<<ans[i]<<endl;
}
原文地址:https://www.cnblogs.com/qscqesze/p/5954393.html