The c ++ implementation of any level magic is odd level magic, dual-even magic, and single-even magic ., Magic single doll
Magic is divided into three categories. Odd Magic Square (odd number), double-even magic square (can be divided by four, such as 8, 12, 16 ......) , Single-doll Magic Square (4 m + 2 forms, such as 6, 10 ......), The constructor algorithms are different.
In the following program, the Construction Algorithm of the odd magic square is the Merzirac method. The Construction Algorithm of the dual Phantom is the Spring method. The Construction Algorithm of the single Phantom is the Strachey method.
Odd magic:
Place 1 in the center of the first row, and enter 2, 3, 4, etc. in the upper right corner ..., If a number exists in the upper-right corner of the page, move it down to another one.
Reference: http://blog.csdn.net/zheng0518/article/details/9006281
Dual-doll magic:
(1) first fill in the numbers in order. Then, divide it by 4*4 into 2*2 small squares.
(2) the number on the diagonal line of each small square matrix is replaced by the number complementary to it.
Reference: http://chenxuebiao3.blog.163.com/blog/static/274911182011111911429621/
Single PHANTOM:
Reference: http://blog.sina.com.cn/s/blog_639b95e90100i6h4.html
The sum of the diagonal lines of each row and column is: sum = n * (n ^ 2 + 1)/2 n is the order.
The Code is as follows:
# Include <iostream> # include <stdlib. h> # include <math. h> using namespace std; int matrix [99] [99] = {0}; // generate an odd number of phantom void CreateOddMagicSquare (int n) {int x = 0, y, mun = 1; y = n/2; while (mun <= n * n) {matrix [x] [y] = mun; // use x0 and y0 to check whether the upper right corner is filled with numbers int x0 = x; int y0 = y; x0 --; y0 ++; // if (x0 <0) x0 + = n; if (y0 = n) y0 = n-y0; if (0 = matrix [x0] [y0]) {x = x0; y = y0;} else {// if there is a number, enter x ++ below the previous number; if (x = n) x = x-n ;} mun ++ ;}// generate dual-idol void CreateDoubleEvenMagicSqure (int n) {int num = 1; // values from 1 to n for (int I = 0; I <n; I ++) for (int j = 0; j <n; j ++) matrix [I] [j] = num ++; // The complement of the number on the diagonal of a small square for (int I = 0; I <n; I ++) for (int j = 0; j <n; j ++) {if (I % 4 = 0 & abs (I-j) % 4 = 0) for (int k = 0; k <4; k ++) matrix [I + k] [j + k] = abs (n * n + 1-matrix [I + k] [j + k]); else if (I % 4 = 3 & (I + j) % 4 = 3) for (int k = 0; k <4; k ++) matrix [I-k] [j + k] = abs (n * n + 1-matrix [I-k] [j + k]);} // generate a single phantom voi D CreateSingleEvenMagicSqure (int n) {int k = n/2; CreateOddMagicSquare (k); // assign an initial value, which is the smallest value in the upper left corner, the second value in the lower right corner, and the second value in the upper right corner, maximum left lower for (int I = 0; I <k; I ++) for (int j = 0; j <k; j ++) {matrix [I + k] [j + k] = matrix [I] [j] + k * k; matrix [I] [j + k] = matrix [I] [j] + k * 2; matrix [I + k] [j] = matrix [I] [j] + k * 3;} // formula n = 4 m + 2 int m = (n-2) /4; // swap the for (int I = 0; I <m-1; I ++) from left to right in the middle of the x direction) {int buf = matrix [k/2] [I]; matrix [k/2] [I] = matrix [k/2 + k] [I]; matrix [k/2 + k] [I] = Buf;} int buf = matrix [k/2] [k/2]; // and the median matrix [k/2] [k/2] = matrix [k/2 + k] [k/2]; matrix [k/2 + k] [k/2] = buf; // exchange m numbers for (int I = 0; I <k; I ++) for (int j = 0; j <k/2; j ++) {if (I! = K/2) {int buf = matrix [I] [j]; matrix [I] [j] = matrix [I + k] [j]; matrix [I + k] [j] = buf ;}// swap the right-most -1 digit for (int I = 0; I <k; I ++) for (int j = n-1; j> n-1-m-1); j --) {int buf = matrix [I] [j]; matrix [I] [j] = matrix [I + k] [j]; matrix [I + k] [j] = buf ;}} // The magic side correctly checks bool Check (int n) {int sum = (n * n + 1)/2; int SumA = 0, SumB = 0; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) sumA + = matrix [I] [j]; if (SumA! = Sum) return false; SumA = 0 ;}for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) SumA + = matrix [j] [I]; if (SumA! = Sum) return false; SumA = 0 ;}for (int I = 0; I <n; I ++) {SumA + = matrix [I] [I]; sumB + = matrix [I] [n-i-1];} if (SumA! = Sum | SumB! = Sum) return false; return true;} int main () {int n; cin> n; if (n % 2! = 0) CreateOddMagicSquare (n); else if (n % 4 = 0) CreateDoubleEvenMagicSqure (n); else if (n % 2 = 0) CreateSingleEvenMagicSqure (n ); for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) cout <matrix [I] [j] <"\ t"; cout <endl;} if (! Check (n) cout <"the ans is wrong" <endl; else cout <"right answer" <endl; system ("pause "); return 1 ;}
Clever filling of single-and double-even-order magic
I. Dual-doll magic solution
The level-n phantom that can be divisible by four is called the dual-even magic, such as the level-8, level-12, and level-16, which is generated by the Spring method and the Strachey method.
1. Generate a dual-doll magic by using the Spring method:
The method is to fill in the number in order, and swap the number in the center symmetric.
Step 1: Enter the number in sequence;
To put it simply, place 1 in any angle of the Magic Square, and then enter the remaining number in sequence in the same direction.
Take the Level 8 Magic Square as an example. As follows:
12345678
910111213141516
1718192021222324
2526272829303132
3334353637383940
4142434445464748
4950515253545556
5758596061626364
There are eight methods. (The following is an example. Other methods are the same)
Step 2: Use the center symmetric interchange number;
There are two symmetric swapping methods:
Method 1: exchange an even number of I + j (row I and column j) in the upper left area with a diagonal number in the lower right corner of the coordinate point in the magic side; in the upper-right corner, the lower-left corner numbers with an odd number of I + j in the image center as the symmetric point are exchanged. (Make sure it is not both odd or even .)
642624559757
955115352145016
4818462021432341
2539273736303432
3331352928382640
2442224445194717
4915511312541056
858660613631
Or,
163361606588
5610541213511549
1747194544224224
4026382829353133
3234303637273925
4123432120461848
165014525311559
577595462264
Complete magic, magic and value 260.
Method 2: divide the magic square into m * m level 4 Magic Square, and diagonal lines (or non-diagonal lines) of the four Magic Square) the number in the square is exchanged with the diagonal number in the n-level magic square that uses the center point as the symmetric point.
In order to exchange the number in the diagonal line of each level 4 magic square with the diagonal number in the n-order magic square with the center point as the symmetric point, the Magic Square, Phantom and value 260 are completed.
642361606757
955541213515016
1747462021434224
4026273736303133
3234352928383925
4123224445191848
4915145253111056
858595462631
In order to exchange the numbers in the square on the non-diagonal lines of each level 4 magic square with the diagonal numbers in the n-order magic square with the center point as the symmetric point, the Magic Square, magic and value 260 are completed.
163624559588
5610115352141549
4818194544222341
2539382829353432
3331303637272640
2442432120464717
165051131254559
577660613264
2. Generate a dual-doll magic square using the Strachey Method
In the first step, the n-level dual-even magic square is represented as the 4 m level magic square. Set the remaining full text to...>
Magic Law
Parity magic
When n is an even number, we call it an even magic square. When n can be divisible by 4, we call this level magic square as a level-4, 8, 12, and 16; when n cannot be divisible by four, we call this level phantom as a single level Phantom, such as Level 6, level 10, and Level 14.
I. Dual-doll magic solution
The level-n phantom that can be divisible by four is called the dual-even magic, such as level-4, level-8, level-12, and level-16. The dual-even magic is generated using the Spring method and Strachey method.
1. Generate a dual-doll magic by using the Spring method:
The method is to fill in the number in order, and swap the number in the center symmetric.
Represent the level n dual-even magic side as the level 4 m magic side. Consider the level n Magic Square as A matrix and record it as a. the number in the square in column j of row I is recorded as A (I, j ).
Step 1, shader a (I, j) = (I-1) * n + j, that is, the first line can be filled in from left to 1, 2, 3 ,...... , N; that is, from left to left, you can enter n + 1, n + 2, n + 3 ,...... , 2n ;............ N ^ 2 [the square of n ].
To put it simply, place 1 in any angle of the Magic Square, and then enter the remaining number in sequence in the same direction.
Step 2: perform symmetric switching.
There are two symmetric exchange methods:
Method 1: swap the even number of I + j in the upper left area with the diagonal number in the lower right corner of the symmetry point in the magic side; in the upper-right corner, the lower-left corner numbers with an odd number of I + j in the image center as the symmetric point are exchanged. (Make sure it is not both odd or even .)
Method 2: divide the magic square into m * m level 4 Magic Square, and diagonal lines (or non-diagonal lines) of the four Magic Square) the number in the square is exchanged with the diagonal number in the n-level magic square that uses the center point as the symmetric point.
Take Level 4 magic as an example:
Step 1: Place 1 in any corner of Level 4 Magic Square, and enter the remaining number in sequence in the same direction. As follows:
1234
5678
9101112
13141516
Or,
15913
261014
371115
481216
There are eight methods. (The first one is used as an example. Other methods are the same)
Step 2: perform symmetric switching. (There are two symmetric swapping methods)
Method 1: the number on the diagonal line (I .e., 1-16, 4-13, 6-11, and 7-10) is symmetric exchanged at the center to complete the magic, magic, and value 34.
162313
511108
97612
414151
Method 2: the number on the non-diagonal line (I .e., 2-15, 3-14, 5-12, 8-9 Interchange) is symmetric exchanged with the center to complete the magic, magic, and value 34.
115144
12679
810115
133216
Take Level 8 magic as an example:
Step 1: Enter the number in sequence. As follows:
12345678
910111213141516
1718192021222324
2526272829303132
3334353637383940
4142434445464748
4950515253545556
5758596061626364
There are eight methods. (The following is an example. Other methods are the same)
Step 2: perform symmetric switching.
There are two symmetric exchange methods:
Method 1: swap the even number of I + j in the upper left area with the diagonal number in the lower right corner of the symmetry point in the magic side; in the upper-right corner, the lower-left corner numbers with an odd number of I + j in the image center as the symmetric point are exchanged. (Make sure it is not both odd or even .)
642624559757
955115352145016
4818462021432341
2539273736303432
3331352928382640
2442224445194717
49155113125410 ...... remaining full text>