Magic Square

来源:互联网 发布:唐泽寿明 知乎 编辑:程序博客网 时间:2024/06/05 11:21

这道题是比较简单的,三个字——找规律。思路也应该是比较清晰的,下面,我们先看一下题目吧。
Description

In recreational mathematics, a magic square of n-degree is an arrangement of n2 numbers, distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. For example, the picture below shows a 3-degree magic square using the integers of 1 to 9.
这里写图片描述
Given a finished number square, we need you to judge whether it is a magic square…….

Input

The input contains multiple test cases.

The first line of each case stands an only integer N (0 < N < 10), indicating the degree of the number square and then N lines follows, with N positive integers in each line to describe the number square. All the numbers in the input do not exceed 1000.

A case with N = 0 denotes the end of input, which should not be processed.

Output

For each test case, print “Yes” if it’s a magic square in a single line, otherwise print “No”……………

Sample Input

2
1 2
3 4
2
4 4
4 4
3
8 1 6
3 5 7
4 9 2
4
16 9 6 3
5 4 15 10
11 14 1 8
2 7 12 13
0

Sample Output

No
No
Yes
Yes
先解释一下大体意思,就是先给你一个数字n,然后是n*n的二维数组,然后让你比较任意列、行、对角线的和是否相等。
从题意中我们就知道代码要分4部分了,1.行的和;2.列的和;3.主对角线的和;4.辅对角线的和。
特别注意
特殊处理n=1的情况;
这是我的代码

#include<stdio.h>#include<string.h>int main(){    int n, i, j, flag, sum1[1000], sum2[1000], a[10][10], sum3, sum4, x;    while(~scanf("%d", &n))    {        memset(sum1, 0, sizeof(sum1));        memset(sum2, 0, sizeof(sum2));        flag=0;        sum3=0;        sum4=0;        if(n==0)        {            break;        }        else if(n==1)        {            scanf("%d", &x);            printf("Yes\n");            continue;        }        for(i=0; i<n; i++)        {            for(j=0; j<n; j++)            {                scanf("%d", &a[i][j]);            }        }        for(i=0; i<n; i++)        {            for(j=0; j<n; j++)            {                if(a[i][j]==a[0][0])                {                    flag=3;                    break;                }                else                {                    flag=0;                }            }        }        for(i=0; i<n; i++)        {            for(j=0; j<n; j++)            {                sum1[i]+=a[i][j];            }        }        for(i=0;i<n;i++)        {            if(sum1[i]!=sum1[0])            {                flag=3;                break;            }        }        for(i=0; i<n; i++)        {            for(j=0; j<n; j++)            {                sum2[i]+=a[j][i];            }        }        for(i=0;i<n;i++)        {            if(sum2[i]!=sum2[0])            {                flag=3;                break;            }        }        for(i=0; i<n; i++)        {            sum3+=a[i][i];        }        for(i=0; i<n; i++)        {            sum4+=a[i][n-i-1];        }        if(flag==3)        {            printf("No\n");        }        else        {            for(i=0; i<n; i++)            {                if(sum1[i]!=sum2[i])                {                    flag=4;                    break;                }                else                {                    flag=0;                }            }            if((flag==0)&&(sum3==sum4))            {                printf("Yes\n");            }            else            {                printf("No\n");            }        }    }    return 0;}

代码看着有点长,但是思路知道了,作对就so easy了。

0 0
原创粉丝点击