Problem: The element of an n-order phalanx is,..., n^2, each of its rows, each column and 2 diagonal elements and equal, so
Square is called Rubik's Cube. n is an odd number when we have 1 constructs, called "upper right", for example, the following gives the n=3,5,7 when the Rubik's Cube.
3
8 1 6
3 5 7
4 9 2
5
17 24 1) 8 15
23 5 7) 14 16
4 6 13) 20 22
10 12 19) 21 3
11 18 25) 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
The 1th line in the middle of the number is always 1, the last 1 lines in the middle of the number is n^2, his right is 2, from the three Rubik's Cube, you can see "right
Above "What it means."
Input
Contains multiple sets of data, first entering T, which indicates that there is a T group of data. Each group of data 1 rows gives an odd number of n (3<=n<=19).
Output
For each set of data, output n-order Rubik's Cube, 4 squares per number, right-aligned
Sample Input
2
3
5
Sample Output
8 1 6
3 5 7
4 9 2
17 24 1) 8 15
23 5 7) 14 16
4 6 13) 20 22
10 12 19) 21 3
11 18 25) 2 9
Reply:
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int main ()
{
int n;
scanf ("%d", &n);
while (n--)
{
int a[max][max]={0};
int i,j,x=1,y,m;
scanf ("%d", &m);
a[1][(m+1)/2]=1;
y= (m+1)/2;
for (i=2;i<=m*m;i++)
{
if (x-1>0&&y-1>0)
{
X=x-1;
Y=y-1;
if (a[x][y]==0)
A[x][y]=i;
else {
x= (x+2)%m;
Y= (y+1)%m;
if (x==0) x=x+m;
if (y==0) y=y+m;
A[x][y]=i;
}
Continue
}
if (x-1<=0&&y-1>0)
{
X=x-1+m;
Y=y-1;
if (a[x][y]==0)
A[x][y]=i;
else {
x= (x+2)%m;
Y= (y+1)%m;
if (x==0) x=x+m;
if (y==0) y=y+m;
A[x][y]=i;
}
Continue
}
if (x-1>0&&y-1<=0)
{
X=x-1;
Y=y-1+m;
if (a[x][y]==0)
{a[x][y]=i;}
else {
x= (x+2)%m;
Y= (y+1)%m;
if (x==0) x=x+m;
if (y==0) y=y+m;
A[x][y]=i;
}
Continue
}
if (x-1<=0&&y-1<=0)
{
X=x-1+m;
Y=y-1+m;
if (a[x][y]==0)
A[x][y]=i;
else {
x= (x+2)%m;
Y= (y+1)%m;
if (x==0) x=x+m;
if (y==0) y=y+m;
A[x][y]=i;
}
Continue
}
}
for (i=1;i<=m;i++)
{
for (j=m;j>=1;j--)
{
printf ("%4d", A[i][j]);
}
printf ("\ n");
}
}
return 0;
}
Odd order Rubik's Cube problem