Sort the natural numbers between 1-N in full order. The Code is as follows:
# Define N 6 # define full-1 // complete void test_row () {int num [N]; // The array bool use [N] to be sorted; // number of records already arranged for (int K = 0; k <n; k ++) {num [k] = 0; use [k] = false ;} row (Num, 0, N, use);} int row (int * num, int K, int N, bool * use) // The order starting from the k Number in num {If (k <0) return full; If (k = N) {for (Int J = 0; j <n; j ++) printf ("% d", num [J] + 1); printf ("\ n") ;}else {int T = 0; for (; t <n; t ++) {If (! Use [T]) {push (T, use); num [k] = T; row (Num, k + 1, n, use); POP (use, num [k]) ;}}} void push (int K, bool * use) // press K into the record {use [k] = true ;} void POP (bool * use, int K) // clear K Records {use [k] = false;
The above program has a problem: it can only start from 1 ...... The State like n is arranged and combined. Now, after improvement, the state can be arranged starting from any State. If the State is incorrect, it can be corrected. The Code is as follows:
# Define N 6 # define full-1 // complete void test_row () {int num [N] = {5, 4 }; // array to be sorted bool use [N]; // The number of records already arranged for (int K = 0; k <n; k ++) {use [k] = false;} row (Num, 0, N, use);} int row (int * num, int K, int N, bool * use) // The order starting from the k Number in num {If (k <0) return full; If (k = N) {bool full = true; for (Int J = 0; j <n; j ++) {printf ("% d", num [J] + 1 ); if (j <n-1 & num [J] <num [J + 1]) Full = false;} printf ("\ n ");} else {int T = num [k]; If (T <0) t = 0; For (; t <n; t ++) {If (! Use [T]) {push (T, use); num [k] = T; row (Num, k + 1, n, use); POP (use, num [k]) ;}} num [k] = 0 ;}} void push (int K, bool * use) // press K into the record {use [k] = true;} void POP (bool * use, int K) // clear the record {use [k] = false ;}
The above program will display all the results and then modify them to display the next combination in the order of display. Here, a parameter flag is added, since there is a clearing process in the returned process in the above program, this flag can indicate whether to clear. In this way, the recursive program determines whether to reset the number to keep the number of sorted items. The Code is as follows:
# Define N 6 # define full-1 // complete # define end 0 # define flag_c 0 // need to be cleared # define flag_n 1 // void test_row () is not required () {int num [N] = {5, 4,}; // The bool use [N] array to be sorted; // The number of records already arranged for (int K = 0; k <n; k ++) {use [k] = false;} row (Num, 0, N, use, flag_c);} int row (int * num, int K, int N, bool * use, int flag) // sorts the number of keys starting from the k Number in num {If (k <0) return full; if (k = N) {for (Int J = 0; j <n; j ++) printf ("% d", num [J] + 1 ); printf ("\ n"); return full;} else {int T = num [k]; If (T <0) t = 0; int sec = 0; For (; t <n; t ++) {If (! Use [T]) {SEC ++; push (T, use); num [k] = T; int F; If (SEC = 2) F = flag_n; else F = flag_c; If (row (Num, k + 1, n, use, f) = END) return end; If (SEC = 2) return end; pop (use, num [k]) ;}} if (flag = flag_c) num [k] = 0 ;}} void push (int K, bool * use) // press K into the record {use [k] = true;} void POP (bool * use, int K) // clear the record {use [k] = false ;}
If there is a problem with the above program, you can run it on your own to see if there is any problem. Here is the code that you think is correct for the moment, which is slightly different:
# Define N 6 # define full-1 // complete # define end 0 # define flag_c 0 // need to be cleared # define flag_n 1 // void show (int * r, int length) {for (Int J = 0; j <length; j ++) printf ("% d", R [J] + 1 ); printf ("\ n");} void test_row () {int num [N] = {5, 1, 2, 4, 3, 0}; // array to be sorted bool use [N]; // number of records already arranged for (int K = 0; k <n; k ++) {use [k] = false;} row (Num, 0, n, use, flag_c);} int row (int * num, int K, int N, bool * use, int flag) // The order starting from the k Number in num {If (k <0) return full; If (k = N) {sho W (Num, n); return full;} else {int T = num [k]; If (T <0) t = 0; int sec = 0; (; T <n; t ++) {If (! Use [T]) {SEC ++; push (T, use); num [k] = T; int F; If (SEC = 2 | flag = flag_n) F = flag_n; else f = flag_c; If (row (Num, k + 1, n, use, f) = END) return end; if (SEC = 2 | flag = flag_n) return end; POP (use, num [k]) ;}} if (flag = flag_c) num [k] = 0 ;}} void push (int K, bool * use) // press K into the record {use [k] = true ;} void POP (bool * use, int K) // clear K Records {use [k] = false ;}