P1978 Magical Magic Square

Source: Internet
Author: User

P1978 Magical Magic SquareDescribe

Magic Square is a very magical n∗n matrix: It is made up of numbers, ..., n∗n, and the sum of the numbers on each row, column, and two diagonal lines are the same.

When N is odd, we can construct a magic square by first writing 1 in the middle of the first line.

Then, fill in each number k (k = 2,3, ..., n∗n) according to the following way from small to large:

    1. if (k−1) in the first row but not in the last column, the K is filled in the last row, (k−1) in the right column of the column;
    2. if (k−1) in the last column but not in the first row, the K is filled in the first column, (k−1) on the row of the previous line;
    3. if (k−1) in the last column of the first row, the K is filled directly below (k−1);
    4. if (k−1) is not in the first line, nor in the last column, if (k−1) is not filled in the upper right, then K is filled in (k−1) in the upper right, otherwise k is filled in (k−1) directly below.

Now given N, please construct n∗n Magic Square according to the above method.

Format input Format

An integer N, which is the size of the magic square.

Output format

The output file contains n rows, n integers per line, that is, the magic side of the N∗n constructed by the above method. Adjacent two integers are separated by a single space.

Example 1 sample input 1[copy]
3
Sample output 1[Copy]
8 1 63 5 74 9 2
Limit

For 100% of data, 1≤n≤39 and N are odd.

Source

NOIP 2015 Raising Group Day 1 first question

Exercises

C++

#include <iostream>#include<cstdio>#include<string.h>using namespacestd;intn,a[ +][ +],x[ the],y[ the];intMain () {scanf ("%d",&N); if(n==1) {printf ("%d\n", N); return 0; } memset (A,0,sizeof(a)); a[1[N-1)/2+1]=1; x[1]=1; y[1]= (n1)/2+1;  for(intI=2; i<=n*n;i++)    {        if(x[i-1]==1&& y[i-1]!=N) {a[n][y[i-1]+1]=i; X[i]=N; Y[i]=y[i-1]+1; Continue; }        if(y[i-1]==n && x[i-1]>1) {A[x[i-1]-1][1]=i; X[i]=x[i-1]-1; Y[i]=1; Continue; }        if(x[i-1]==1&& y[i-1]==N) {a[2][n]=i; X[i]=2; Y[i]=N; Continue; }        if(x[i-1]!=1&& y[i-1]!=N) {if(a[x[i-1]-1][y[i-1]+1]==0) {A[x[i-1]-1][y[i-1]+1]=i; X[i]=x[i-1]-1; Y[i]=y[i-1]+1; Continue; }            Else{a[x[i-1]+1][y[i-1]]=i; X[i]=x[i-1]+1; Y[i]=y[i-1]; Continue; }        }    }     for(intI=1; i<=n;i++)    {         for(intj=1; j<n;j++) {printf ("%d", A[i][j]); } printf ("%d\n", A[i][n]); }    return 0;}

P1978 Magical Magic Square

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.