# C language | output magic cube matrix

2021-01-20 12:21:24

example 63：C Language implements output “ Rubik's Cube ”. The so-called magic square array refers to every line of it , The sum of each column and diagonal is equal .

Their thinking ： The arrangement of the numbers in the magic square matrix , The order of the matrix should be odd .

take 1 Put it in the 1 A column in the middle of the row
from 2 Start until n*n Press each number in turn ： The number of rows stored in each number is less than that of the previous number 1, The number of columns plus 1.
If the behavior of the previous number is 1, Then the number of rows of the next number is n
When the number of columns of the last number is n when , The number of columns of the next number should be 1, The number of rows minus 1
According to the above rules, there are already numbers in the position , Or the last number is 1 Xing di n Column time , Put the next number below the previous number

Source code demo ：

``````#include<stdio.h>// The header file
int main()// The main function
{

int a[20][20]={

0};// Define a two-dimensional array
int i,j,n,k; // Defining integer variables
i=1;// assignment
printf(" Please enter order as 1~15 Between the odd numbers ：\n");// Prompt statement
scanf("%d",&n);// Enter the dimensions of the cube n
j=n/2+1; // j It's half the dimension plus 1.
a[i][j]=1; // Make sure the number in the middle of the first row is 1
for(k=2;k<=n*n;k++)// It has been determined that 1 Location. , I'm sure 2~n*n The location of
{

i=i-1; // Move , One up in the vertical row .
j=j+1; // Move , Move one to the right .
if((i<=0)&&(j<=n)) // If the vertical row moves to the top , At the same time, the horizontal row has not exceeded the rightmost , The vertical row goes to the bottom and then continues .
{

i=n;
}
if((i<=0)&&(j>n)) // If the vertical row moves to the top , At the same time, the horizontal row exceeds the rightmost , Move down the vertical two , Move the row one bit to the left .
{

i=i+2;
j=j-1;
}
if(j>n) // If only the horizontal row exceeds the rightmost , Move to the second line on the left .
{

j=1;
}
if(a[i][j]==0)
{

a[i][j]=k; // If this position has not been assigned yet , Then the assignment is k.
}
else  // It has been assigned . Then move the vertical row down two , Move the row one bit to the left , Then assign the value to k.
{

i=i+2;
j=j-1;
a[i][j]=k;
}
}
for(i=1;i<=n;i++) // Cycle output position .
{

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

printf("%3d ",a[i][j]);
}
printf("\n");// Line break
}
}
``````

The results of compiling and running are as follows ：

`````` Please enter order as 1~15 Between the odd numbers ：
3
8   1   6
3   5   7
4   9   2

--------------------------------
Process exited after 1.327 seconds with return value 3
Please press any key to continue . . .
``````

above , If you read it and think it helps you , Just give Xiao Lin a compliment , Share it with the people around you , In this way, Kobayashi has the power to renew itself , Thank you on your knees ~

C Language output magic cube matrix
More cases can go official account ：C From language introduction to mastery

https://chowdera.com/2021/01/20210120115707851v.html