Three methods of generating arbitrary odd order magic squares

Source: Internet
Author: User

1. Magic square Matrix

Magic Square is a very interesting digital matrix, in the early famous nine gossip array on the magic side.

The magic square is defined as:

1 to n*n integers are filled in the squares of the n*n, and the sum of the numbers of each row and column and diagonal must be equal.

You, as the top programmer at the gossip company, now need you to solve a problem and find any odd-numbered magic square.

Input:

 

The input includes multiple test sets, one positive odd n (1<= n < 1000) for each behavior, and 0 as the end of the input and does not need to be processed.

 

Output:

 

For each n input, output one of its corresponding n-order magic Square, if there are multiple, any one can.

Each magic square is a matrix of n*n,

For each magic square, a row of magic squares is output per line, with one or more spaces separating the digits in each row. Different magic squares are separated by a blank line.

Algorithm idea:

The problem is also implemented in three ways.

Main interface:

Generation of odd-order magic squares by Merzirac method

Algorithm idea:

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

Input 1: Create a 5*5 Rubik's Cube, this Rubik's Cube is fixed. You can change the n in the macro definition to produce any odd order Rubik's Cube.

The Loubere method generates the odd order magic square.

Algorithm idea:

In the center of the square up in a box 1, in turn, fill 2, 3, 4 ... if there is already a number on the top right, move up two squares and continue filling

Input 2: Create a 5*5 Rubik's Cube, this Rubik's Cube is fixed. You can change the n in the macro definition to produce any odd order Rubik's Cube.

The horse method generates the odd order magic square.

Algorithm idea:

Put 1 in any grid first. Take 1 steps to the left, and go down 2 steps into 2 (called horse stance), walk 1 steps to the left, and go 2 steps into 3, and then put to N. Place the n+1 below N (called a jump), and then put the above method into the 2n, placing the 2n+1 at the bottom of the 2n.

Input 3: Prompt, enter a data, at this time the input data indicates how many different fantasy parties randomly generated, but also can change the definition of the "n" Create fantasy side.

Enter 0: Any key to exit.

Attachment:

#include <stdio.h>

#include <windows.h>

#include <time.h>

void Merziracmagic ();

void Magichorserandom ();//Randomization method

void Louberemagic ();

int num (int number); The number of magic squares produced by Magichorserandom () Randomization method

int Menu_select ();

#define N 5//magic square must be odd

int Menu_select ()

{

char c;

System ("CLS");/* Run the splash screen/*

printf ("\t\t**** Rubik's Cube Author byyongliu ****\n");

printf ("\t\t 1. Merziracmagic Rubik's Cube |\n ");

printf ("\t\t 2. Louberemagic Rubik's Cube |\n ");

printf ("\t\t 3. Magichorserandom Rubik's Cube |\n ");

printf ("\t\t 0. Exit |\n ");

printf ("\t\t*****************************************\n");

printf ("\t\t\t Please Choose (0-3):");

do{

C=getchar (); /* Read the choice * *

}while (c< ' 0 ' | | C> ' 3 ');

Return (c ' 0 '); /* Return selection * *

}

int main ()

{

int number;

for (;;)

{

Switch (Menu_select ())//* Select Judgment * *

{

Case 1:

printf ("\t\t1. Merziracmagic Rubik's Cube |\n ");

Merziracmagic ();

Break

Case 2:

printf ("\t\t2. Louberemagic Rubik's Cube |\n ");

Louberemagic ();

Break

Case 3:

printf ("\t\t3. Magichorserandom Rubik's Cube |\n ");

printf ("\t\tpleaseinput The number of magic");

scanf ("%d", &number);

Num (number);//Randomization method

printf ("\t\t\t");

Break

Case 0:

printf ("\t\t\t End Exit!\n"); /* END PROCEDURE * *

printf ("\t\t\t");

System ("pause");

Exit (0);

}

}

}

int num (int number)

{

inti=0;

intcount=0;

Srand ((int) time (NULL));

while (++count<=number)

{

printf ("\t\t%d.\n", ++i);

Magichorserandom ();

}

RETURN1;

}

void Magichorserandom ()

{

inta[n+1][n+1]={0};//to initialize it

Inti=rand ()%n+1; IJ randomly selected in 1 to N*n

Intj=rand ()%n+1;

printf ("%2d\n", I);

printf ("%2d\n", j);

A[i][j]=1;

for (intk=2;k<=n*n;k++)

{

int row=i;

int col=j;

++j; Take one step to the right

if (j>n)

J=1;

++i; Take a step down

if (i>n)

I=1;

++i; Take a step down

if (i>n)

I=1;

if (a[i][j]==0)//If there is no element in the upper right to fill in

A[i][j]=k;

Else

{

I=row; Remember subscript

J=col;

i++;

if (i>n)

I=1;

A[i][j]=k;

}

}

for (i=1;i<=n;i++)

{

printf ("\t\t");

for (j=1;j<=n;j++)

{

printf ("%3d", A[i][j]);

}

printf ("\ n");

}

printf ("\ n");

printf ("\ n");

}

Generation of odd-order magic squares by Merzirac method

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

void Merziracmagic ()

{

inta[n+1][n+1]={0};//to initialize it

inti=1,j= (n+1)/2;

A[i][j]=1; The middle element of the first line is placed 1

for (intk=2;k<=n*n;k++)

{

int row=i;

int col=j;

I.;

++j; Fill in the top right element continuously

if (i==0)

I=n;

if (j>n)

J=1;

if (a[i][j]==0)//If there is no element in the upper right to fill in

A[i][j]=k;

Else

{

I=row; Remember subscript

J=col;

i++;

if (i>n)

I=1;

A[i][j]=k;

}

}

for (i=1;i<=n;i++)

{

printf ("\t\t");

for (j=1;j<=n;j++)

{

printf ("%3d", A[i][j]);

}

printf ("\ n");

}

printf ("\ n");

printf ("\ n");

}

Generation of odd-order magic squares by Loubere method

In the center of the square up in a box 1, in turn, fill in 2, 3, 4 ..., if there is already a number on the top right, move up two squares to continue filling.

void Louberemagic ()

{

inta[n+1][n+1]={0};//to initialize it

Inti= (n+1)/2-1,j= (n+1)/2;

A[i][j]=1; The upper layer of the most intermediate element

for (intk=2;k<=n*n;k++)

{

int row=i;

int col=j;

I.;

++j; Fill in the top right element continuously

if (i==0)

I=n;

if (j>n)

J=1;

if (a[i][j]==0)//If there is no element in the upper right to fill in

A[i][j]=k;

Else

{

I=row; If you use backtracking methods to consider the problem more, or remember subscript

J=col;

i--;

if (i==0)

I=n;

i--;

if (i==0)

I=n;

A[i][j]=k;

}

}

for (i=1;i<=n;i++)

{

printf ("\t\t");

for (j=1;j<=n;j++)

{

printf ("%3d", A[i][j]);

}

printf ("\ n");

}

printf ("\ n");

printf ("\ n");

}

Note: Code uploaded in http://download.csdn.net/detail/liuyongvs2009/7077279

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.