"One" Magic square Description:
Rubik's Cube array is a n*n matrix;
Each row of the matrix, each column, the sum of the diagonal are equal;
Example of the second magic Phalanx:
Third Order Magic Phalanx:
8 1 6
3 5 7
4 9 2
The sum of each line: 8+1+6=15;
3+5+7=15;
4+9+2=15;
The sum of each column: 8+3+4=15;
1+5+9=15;
6+7+2=15;
Diagonal sum: 8+5+2=15;
6+5+4=15;
"Three" magic square calculation Law (row, column starting at 1):
1. Place "1" in the first row, one column in the middle;
2. From 2 onwards to n*n the following rules:
Each number of rows is reduced by 1 compared to the previous number of rows;
Each number of columns is 1 more than the previous column;
3. When a number behaves 1, the next number behaves n;
4. When a sequence number is n, the next number of columns is 1, and the number of rows is reduced by 1;
5. If the position determined by the above rules has a number, or the previous digit 1th row n,
The next digit position is directly below the previous number (the number of rows minus 1, the number of columns is unchanged);
"Four" source code:
1 #define_crt_secure_no_warnings2#include <stdio.h>3#include <stdlib.h>4#include <math.h>5 6 #defineN 57 8 intMain ()9 {Ten intA[n][n] = {0}; One intCount =1; A introw =0, cul = N/2; - while(Count <= NN) - { theA[row][cul] =count; - inti =Row; - intj =Cul; - if(i = =0) + { -i = N-1; + } A Else at { -i = i-1; - } -j = (J +1) %N; - if(a[i][j]!=0|| (row==0&&cul==n-1)) - { ini = row +1; -j =Cul; to } +row =i; -Cul =J; thecount++; * } $ Panax Notoginseng for(inti =0; i < N; i++) - { the for(intj =0; J < N; J + +) + { Aprintf"%3d", A[i][j]); the } +printf"\ n"); - } $ $System"Pause"); -}
View Code
C language-Print magic squares (each row, each column, the sum of the diagonal equals)