One interesting C program gives the square array order number

xiaoxiao2021-03-05  18

It is said to be a question on the computer newspaper: give a square matrix from the outer layer to the inner layer one by one, the results are as follows: (8 * 8 square array)

1 28 27 26 25 47 46 45 44 21 3 30 49 60 59 58 43 20 4 31 50 61 64 57 42 19 5 32 51 62 63 56 41 18 6 33 52 53 54 55 40 17 7 34 35 36 37 38 39 16 8 9 10 11 12 13 14 15

I tried to use the recursive implementation, and then convert it into non-recursion. Maybe not to say the most, but hope to throw the jade, you can see more exciting implementation!

Recursive algorithm:

/ * By zzmdl, 2005-4-6 * /

#define n 8 / * Define the size of the square * / void print_Array (int, int arch []; void draw (int, int, int arch;);

Main () {INT I, J, Array [N] [N]; DRAW (n, 0, 1, array); Print_Array (n, array);}

Void Print_Array (INT N, INT Array [N]) {INT I, J; For (i = 0; I

Void Draw (INT N, INT Level, INT K, INT Array [N]) {INT A; if (Level> N / 2) Return; / * Level is used to record layer layer is being numbered * / Else {for (a = level; a = level; A -) {Array [A] [N-Level-1] = K; K ;} / * Edible; * / for (a = n-2-level; a> = level 1; a -) / * Edge; * / {Array [level] [a] = k; k ;} DRAW (N, Level 1, K, Array); / * Recursive call * /}}

Non-created algorithm:

/ * By zzmdl, 2005-4-6 * /

#define n 8void Print_Array (int, int Array [] []); void draw (int, int Array []);

Main () {INT I, J, Array [N] [N]; Draw (1, array); Print_Array (n, array);}

Void Print_Array (INT N, INT Array [N]) {INT I, J; For (i = 0; I

Void Draw (int K, int Array [n]) {Int a; int next = 0; while (level <= n / 2) {for (a = level; c-level; a ) {Array [A] [level] = k; k ;} for (a = level 1; a = level; A-) {Array [A] [N-Level-1] = K; K ;} for (a = n-2-level; a> = level 1; A-) {Array [Level] [A] = K; K ;} Level ;}} Continued: From the above two implementations, it can be seen that the recursive algorithm is easy to understand and implement, but it can be tested that the speed is slower. Non-created algorithm.

However, I did an experiment today, I found that the speed is the same, I don't know what is the problem, being dealing with it!

转载请注明原文地址:https://www.9cbs.com/read-39003.html

New Post(0)