magic cube
topic description
The magic square is a very magical N∗NN*NN∗N matrix: it consists of numbers1,2,3,⋯⋯,N×N1,2,3,\cdots \cdots ,N \times N1,2,3,⋯⋯,N×N is formed, and the sum of the numbers in each row, column and two diagonals is the same.
Our NNWhen N is an odd number, we can construct a magic square by the following method:
First set 111 is written in the middle of the first line.
After that, fill in each number K (K=2,3,⋯ ,N×N)K (K=2,3,\cdots,N \times N) in ascending order as followsK(K=2,3,⋯,N×N) :
- If (K−1)(K-1)(K−1 ) in the first row but not in the last column, then putKKK is filled in the last line,(K−1)(K-1)(K−1 ) the right column of the column;
- If (K−1)(K-1)(K−1 ) in the last column but not in the first row, thenKKK is filled in the first column,(K−1)(K-1)(K−1 ) the line above the line where it is located;
- If (K−1)(K-1)(K−1 ) In the last column of the first row,KKK is filled in(K−1)(K-1)(K−1 ) directly below;
- If (K−1)(K-1)(K−1 ) is neither in the first row nor in the last column, if(K−1)(K-1)(K−1 ) If the number on the upper right side has not been filled yet, thenKKK is filled in(K−1)(K-1)(K−1 ) to the upper right, otherwiseKKK is filled in(K−1)(K-1)(K−1 ) directly below.
Now given NNN , please constructN×NN \times NN×N 's magic square.
enter
a positive integer NNN , the size of the magic square.
output format
Total NNN lines, each lineNNN integers, that is, N×NN \times Nconstructed according to the above methodN×A magic square of N , separated by a single space between two adjacent integers.
Input and output samples
illustrate
For 100%100\%100% of the data, for all data,1≤N≤391 \leq N \leq 391≤N≤39 andNNN is an odd number.
NOIp2015 raise group d1t1
Simple AC
#include<bits/stdc++.h>
using namespace std;
long long q,w,e,r,t,y,u,o,s,d,f,g,h,j,z,x,c,v,n,m,i;
long long k,l,p;
long long b[10000],a[10000][10000];
int main()
{
cin>>n;
x=1;y=n/2+1;
for(i=1;i<=n*n;i++)
{
if(a[x][y]>=1)
{
y--;
x+=2;
if(x>n)
x-=n;
if(y==0)
y=n;
}
a[x][y]=i;
x--;y++;
if(x==0)
x=n;
if(y==n+1)
y=1;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}