One: Odd magic square algorithm:
1. The first element is placed in the middle column of the first row
2. The next element is stored in the previous row and next column of the current element.
3. If the previous row, the next column already has content, the next element is placed in the next row in the forefront.
When looking for a row, the next row, or the next column, the matrix must be treated as a wrap.
Algorithm implementation:
#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<math.h>#defineMAXSIZE 100intMain () {//Output Magic Phalanx intn,i,j; intRow, col; intLrow, LCol;//Save the previous step data for restore intA[maxsize][maxsize] = {0 }; while(1) {printf ("Print a odd number: (3-99)"); scanf ("%d", &N); if(n%2) Break; } row=0; Col = (n-1)/2; A[row][col] =1; for(i =2; I <= n*n; i++) {row--; Col++; if(Row <0) Row= N-1; if(Col >=N) Col=0; if(A[row][col])//If there is data in the next row, we will restore the subscript, the number of rows plus one{row= Lrow +1; Col=LCol; if(Row >=N) Row=0; } lcol=Col; Lrow=Row; A[row][col]=i; } for(i =0; I < n; i++) { for(j =0; J < N; J + +) printf ("%5d", A[i][j]); printf ("\ n"); } System ("Pause"); return 0;}
Two: Order n = 4 * m (M =1,2,3 ... The law of the even Rubik's Cube is as follows:
Numbers from small to large, that is, 1,2,3... n2 order to fill the magic phalanx from left to right, from top to bottom, and divide the magic phalanx into several 4x4 sub-matrices, and take out the elements on the diagonal of the sub-square. ; The removed elements are filled in order from large to small to the vacant place of the NxN square.
Algorithm implementation:
#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<math.h>#defineMAXSIZE 100intMain () {//Output Magic Phalanx intn,i,j; intRow, col; intA[maxsize][maxsize] = {0 }; inttemparray[MAXSIZE*MAXSIZE/2] = {0};//main diagonal for storing individual sub-matrices while(1) {printf ("print a even number: (4-100)"); scanf ("%d", &N); if(n%4==0) Break; } //Step One: Populate the data sequentiallyi =1; for(row =0; Row < n; row++) for(col =0; Col < n; col++) A[row][col]= i++; //Step Two: The data are all divided into 4x4 sub-square, take out the main diagonal, sorted by size. Note: The data obtained here is already small to large.i =0; for(row =0; Row < n; row++) { for(col =0; Col < n; col++) { if(Col%4= = row%4) || (Col%4+ Row%4) ==3) ) {Temparray[i]=A[row][col]; I++; } } } //Step Three: Put the data from large to small on the diagonal of the previous sub-squarei--; for(row =0; Row < n; row++) { for(col =0; Col < n; col++) { if(Col%4= = row%4) || (Col%4+ Row%4) ==3) ) {A[row][col]=Temparray[i]; I--; } } } //Step four: Output magic Phalanx for(i =0; I < n; i++) { for(j =0; J < N; J + +) printf ("%5d", A[i][j]); printf ("\ n"); } System ("Pause"); return 0;}
Third: order n
= 4
*M
+2 (M =1,2,3 ... ) Magic cube (single-even cube)
The Rubik's Cube is divided into a, B, C, D four K-order matrices, the four matrices are odd square, using the method described above, in turn A, D, B, C filled with odd Rubik's Cube. Swap A, C Rubik's Cube element, to the middle row of the cube, exchange the corresponding elements from the middle column to the right of the M column, and to the other rows, swap the corresponding elements of the left-to-right m column. Swaps the B, D Rubik's Cube elements, exchanging the corresponding elements from the middle column to the left m–1 column.
#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<math.h>#defineMAXSIZE 6intMain () {//Output Magic Phalanx intN, I, J, K,temp; intRow, col; intLrow, LCol; intA[maxsize][maxsize] = {0 }; while(1) {printf ("print a even number:4*m+2<m=1,2,... >"); scanf ("%d", &N); if(n%4==2) Break; } //Step One: Build four sub-matrices Adbc//build A and then add each element to a to get all the sub-matricesK = n/2; Row=0; Col= (K-1) /2; A[row][col]=1; for(i =2; I <= k*k;i++) {row--; Col++; if(Row <0) Row= K-1; if(Col >=k) Col=0; if(A[row][col]) {row= Lrow +1; Col=LCol; if(Row >=k) Row=0; } lcol=Col; Lrow=Row; A[row][col]=i; } //constructing DBC matrices in sequence for(row =0; Row < k;row++) { for(col =0; Col < k;col++) {A[row+ K][col + K] = A[row][col] + k*k;//D Sub-phalanxA[row][col + K] = A[row][col] +2*k*k;//B Sub-phalanxA[row + K][col] = A[row][col] +3*k*k;//C Sub-phalanx } } //Step Two: Exchange data of the AC sub-phalanx//1. First swap the middle row of the right half of the M column,n=2* (2*m+1), k=2*m+1,m= (k-1)/2, so here M is not included in the last column, including the middle column//2. For other rows, the first half of each row will be for(row =0; Row < k;row++) { if(row = = k/2)//Middle Row { for(col = k/2; Col < K-1; col++) {Temp=A[row][col]; A[row][col]= A[row +K] [Col]; A[row+ K][col] =temp; } } Else //other rows, before the interchange m column, not including the middle column { for(col =0; Col < K/2; col++) {Temp=A[row][col]; A[row][col]= A[row +K] [Col]; A[row+ K][col] =temp; } } } //Step three: Swap the BD sub-phalanx, swap the middle column to the left m-1 column for(row =0; Row < k;row++) { for(i =0; I < (K-1) /2-1; i++) {Temp= A[row][k + k/2-i]; A[row][k+ K/2-I] = A[row + k][k + k/2-i]; A[row+ K][k + k/2-I] =temp; } } //Step four: Output magic Phalanx for(i =0; I < n; i++) { for(j =0; J < N; J + +) printf ("%5d", A[i][j]); printf ("\ n"); } System ("Pause"); return 0;}
C Language Review---output magic square