Output of rank n cube array (odd array)

Source: Internet
Author: User

Requirement

Output 1 ~ The Natural Number of n² constitutes a cube array.

STEP 1 What is cube?

Cube array, also known as "Landscape Chart" in ancient times, refers to the composition elements of natural numbers 1, 2... N × n square matrix of n2, where each element value is not equal, and the sum of n elements in each row, column, and primary and secondary diagonal lines is equal.

STEP 2 What are the laws of cube arrays?

Here we will first write a simple odd-order cube array. the even-order algorithms are more complex and will not be discussed for the moment.

How to arrange the odd rank cube array:

(1) Place 1 in the middle column of the first row;

(2) The numbers starting from 2 until n × n are stored in sequence according to the following rules. The number of rows in each number is less than the number of rows in the previous number, and the number of columns is increased by 1;

(3) If the number of rows in the previous row is 1, the number of rows in the next row is n (the lowest row );

(4) when the number of columns in the previous row is n, the number of columns in the next row is 1, and the number of rows minus 1;

If the previous number is the n column of the first row, the next number is placed under the previous number.

STEP 3 programming ideas

(1) construct a prototype of an n-level cube array, that is, a dynamic two-dimensional array of n * n;

(2) Compile functions to implement the Arrangement Algorithm of cube arrays;

(3) call a function to output cube arrays.

STEP 4 Key Points

Malloc function: allocate memory space to dynamic arrays (use the free statement at the end of the function to release the occupied memory)

Memset function: assigns initial values to all elements of the array.

 

----------------------------------------- Lili split line ------------------------------------- the code is about to appear -----------------------------------

 

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 5 void f (int x); 6 7 int main () 8 {9 int x; 10 11 scanf ("% d", & x ); 12 f (x); 13 14 return 0; 15} 16 17 void f (int x) 18 {19 int I, m, n; 20 int ** mf; 21 22 mf = (int **) malloc (sizeof (int *) * x); // defines a dynamic two-dimensional array mf23 24 for (I = 0; I <x; I ++) 25 {26 mf [I] = (int *) malloc (sizeof (int) * x); 27 memset (mf [I], 0, sizeof (int) * x); // initialization, which assigns 0 values to all elements of the two-dimensional array mf. 28} 29 30 m = 0; 31 n = x/2; 32 mf [m] [n] = 1; 33 34 for (I = 2; I <= x * x; I ++) 35 {36 m --; 37 n ++; 38 39 if (m <0) // if it is in the first row, the number of rows is changed to the last row, and the number of columns is increased by 1 40 {41 m = x-1; 42} 43 44 if (n> x-1) // if it is in the last column, the number of rows is reduced by 1, and the number of columns is 1st rows 45 {46 n = 0; 47} 48 49 if (mf [m] [n]! = 0) // if the subsequent number is already in place, the number of rows is increased by 1, and the number of columns remains unchanged at 50 m ++; 51 52 mf [m] [n] = I; 53} 54 55 for (m = 0; m <x; m ++) 56 {57 for (n = 0; n <x; n ++) 58 {59 printf ("% 5d", mf [m] [n]); 60} 61 62 printf ("\ n "); 63} 64 65 for (I = 0; I <x; I ++) // release the occupied memory 66 {67 free (mf [I]); 68 mf [I] = 0; 69} 70 71 free (mf); 72 mf = 0; 73}



Related Article

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.