First, let's talk about the cube array.
1. Rubik's Cube array
First of all, what's the cube array? For example:
6 1 8
7 5 3
2 9 4
Each of his rows, each column, and the sum of the diagonals equals a constant, which is n (n*n+1)/2. where n is the order number.
So how do we implement the cube array?
1. We place 1 in the middle position of the first line, and (i,j) = (1, (n+1)/2)
2. The next number we write above the previous number of the main diagonal, i.e. (i1,j1) = (i-1,j-1)
3. If I and J are out of bounds in the previous step, make I=n or j=n.
4. If the position that should be written is not out of bounds, but there is already a number, move him to the next line, but the number of columns does not change.
The code is as follows: Environment VS2010
<span style= "FONT-SIZE:24PX;" >int a[100][100];int n;//scanf ("%d", &n); int i=1;int j=int ((n+1)/2); int I1,j1;int X=1;memset (a,0,sizeof (a)); while (x<=n*n) {a[i][j]=x;x++;i1=i;j1=j;i--;j--;if (i==0) {i=n;} if (j==0) {j=n;} if (a[i][j]!=0) {i=i1+1;j=j1;}} for (i=1;i<=n;i++) {printf ("\ n"), for (j=1;j<=n;j++) {printf ("%4d", A[i][j]);}} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fun Array (i)