Fun Array (i)

Source: Internet
Author: User

First, let's talk about the cube array.

1. Rubik's Cube array

First of all, what's the cube array? For example:

6 1 8

7 5 3

2 9 4

Each of his rows, each column, and the sum of the diagonals equals a constant, which is n (n*n+1)/2. where n is the order number.

So how do we implement the cube array?

1. We place 1 in the middle position of the first line, and (i,j) = (1, (n+1)/2)

2. The next number we write above the previous number of the main diagonal, i.e. (i1,j1) = (i-1,j-1)

3. If I and J are out of bounds in the previous step, make I=n or j=n.

4. If the position that should be written is not out of bounds, but there is already a number, move him to the next line, but the number of columns does not change.

The code is as follows: Environment VS2010

<span style= "FONT-SIZE:24PX;" >int a[100][100];int n;//scanf ("%d", &n); int i=1;int j=int ((n+1)/2); int I1,j1;int X=1;memset (a,0,sizeof (a)); while (x<=n*n) {a[i][j]=x;x++;i1=i;j1=j;i--;j--;if (i==0) {i=n;} if (j==0) {j=n;} if (a[i][j]!=0) {i=i1+1;j=j1;}} for (i=1;i<=n;i++) {printf ("\ n"), for (j=1;j<=n;j++) {printf ("%4d", A[i][j]);}} </span>



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fun Array (i)

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.