The C + + implementation of any order magic square----odd-order magic square, double-even magic square, and single-even magic square.

Source: Internet
Author: User

Magic Square is divided into Class 3 . Odd Order magic square (odd), double even magic square (can be divisible by 4, such as 8,12,16 ...). ), the single magic square (4m+2 form, such as 6,10 ... ), the construction algorithm is different.

In the following program, the algorithm for constructing odd-order magic squares is Merzirac method . The construction algorithm of double-even magic square is Spring method . The construction algorithm of the single-dipole magic square is Strachey method .


Single Magic square:

Place 1 in the center of the first row, and then fill in 2, 3, 4 ... if there is already a number on the top right, move down one of the fields to continue.

Reference: http://blog.csdn.net/zheng0518/article/details/9006281


Double Even magic square:

(1) First fill in the numbers in order. Then, press 4*4 to split it into 2*2 small square.

(2) The number on the diagonal of each small square, replaced by a number complementary to it.

Reference: http://chenxuebiao3.blog.163.com/blog/static/274911182011111911429621/


Single Magic square:

Reference: http://blog.sina.com.cn/s/blog_639b95e90100i6h4.html


The and formula of the diagonal of each column is:sum=n* (n^2+1)/2 n is the order number


The code is as follows:

#include <iostream> #include <stdlib.h> #include <math.h>using namespace Std;int matrix[99][99] = {0 };//Generate Odd magic square void Createoddmagicsquare (int n) {int X=0,y,mun =1;y=n/2;while (mun <= n*n) {Matrix[x][y] = mun;//by x0, y0 detect right is already populated with the number int x0=x;int y0=y;x0--;y0++;//to handle if (x0<0) x0+=n;if (y0 = = N) y0= n-y0;if (0 = = Matrix[x0][y0]) {x = X0;y = y0; }else{//x++;if (x = = N) x = X-n If a number is filled in before the number. Mun + +;}} Generates double-even magic square void createdoubleevenmagicsqure (int n) {int num = 1;//from 1 to n in order (int i=0;i<n;i++) for (int j=0;j<n;j++ ) Matrix[i][j] = num++;//The number on the diagonal of the small square takes its complement 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]);}} Generates a single-even magic square void createsingleevenmagicsqure (int n) {int k = N/2; Createoddmagicsquare (k);//Assign initial value, top left minimum, lower right second, right up again, left down Max 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*k*2;matrix[i+k][j] = Matrix[i][j] + k*k*3;} Formula n=4m+2 int m = (n-2)/4;//interchange x direction positive row in left-to-right m-1 for (int i=0;i<m-1;i++) {int buf = Matrix[k/2][i];matrix[k/2][i] = Matri X[k/2+k][i]; Matrix[k/2+k][i] = buf;} int buf = matrix[k/2][k/2];//and positive middle number MATRIX[K/2][K/2] = MATRIX[K/2+K][K/2];MATRIX[K/2+K][K/2] = buf;// Swap other rows except x positive middle row corresponding number m 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 rightmost m-1 digits 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;}} Magic Square correctly checks bool Check (int n) {int sum = (n (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<<endl ;} if (! Check (n)) cout<< "The ANS is wrong" <<endl;else cout<< "right answer" <<endl; System ("pause");  return 1;}



The C + + implementation of any order magic square----odd-order magic square, double-even magic square, and single-even magic square.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.