A better full-aligned algorithm (C language)

zhaozj2021-02-17  52

Hand-aligned algorithm

I have a better full-routing algorithm, I verified that the results of 3, 4, and 5 are correct.

There is no recursive in the program, just a few cycles, and the speed is also satisfactory. On the C466A, Win2000 machine, 8 digital full-rated, the result is not displayed, redirect into a text file, time consuming less than a second

. The 9-digit full-line consumption of 6 seconds. 10 numbers of all-signed 55 seconds. (All the results are not displayed, and all over a text file)

The 11 digits of all-alone (no results are displayed in the original program) time consumption of approximately 16 seconds.

.

Welcome to your guidance

The algorithm is: use two arrays, one array stores the current result, and the second array is the flag that has been used by each number. For example, 10 numbers

The first result is: 0 1 2 3 4 5 6 7 8 9. Then set the usage flag of each number to 1. Then start the main loop: Print the current result first. In the result of the previous result, you will find the first number from a small to large. Every time you find a logo of the number on the current position

For 0, then find the first number than this number but there is no number. For example, the previous result is: 4 1 9 7 8 2 5 0 6 3, then the first number from the previous one is 0, the first one is more than 0 but no

The number used is 3 (because only 6 and 3 are more than 0). Finally, the remaining number has not been used sequentially sequentially. So the next one

The result is 4 1 9 7 8 2 5 3 0 6.

The source program is (compiled in BC 3.0):

#include / * These two library functions are habitual plus ^ _ ^. * / # include

#define isprint / * Whether to print the result of the result * / # define max 200 / * maximum number * /

Unsigned int * _num; / * An array pointer for storing a result * / char * _numflag; / * Used to store the flag that has been used * /

#define Num (j) (* (_ Num (j))) / * 第 位 号 * / # define Numflag (j) (* (_ Numflag (j))) / * Number J Is the flag that has been used, 0 In use, 1 is already using * / # define number (j) (* (_ numflag (* (_ Num (j))))) / * No flag on the number of numbers, 0 is not used, 1 is already

Use * /

Void main () {unsigned int number, j; int i; printf ("/ ninput number ="); scanf ("% u", & number); if ((Number> = max) || (Number <= 1) ) {PUTS ("Enter data error."); exit (-1);}

/ * Initialization of memory and first result * / _num = (unsigned int *) malloc (SizeOf (unsigned int) * number); if (! _ Num) {PUTS ("Assignment to _NUM's memory is insufficient"); EXIT -1);} _numflag = (char *) malloc (sizeof (char) * number); if (! _ Numflag) {PUTS ("Distribution to _Numflag has insufficient memory"); exit (-1);}

For (i = 0; i

/ * Looking for the first number from the previous result from a small to a large number, and stored in the variable j * / for (i = Number-2; I> = 0; i -) {Numuse I) = 0; IF (Num (i)

IF (i <0) Break; / * exits the main loop from here. * /

For (j = NUM ​​(i) 1; j

Numflag (j) = 1; NUM (i) = j;

For (j = 0, i ; i

/ * Problem end * /

Of course, the speed of this program is not the fastest, and there is a big optimization in the program, and some loops can be reduced, or other better algorithms.

Download source program http://263.9cbs.net/filebbs/files/2001_8/t_387_1.zip

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

New Post(0)