Uploaded
Hzyqazasdf
This question has been done before; it is done using the next_permutation function in STL. I can't remember this question again today, I have learned something before, but I have to review it for a while. Today I have done it again with DFS search + backtracking recursion. The key is to train my thinking skills;
The next_permutation function is used to output all the columns larger than the current one. The order is from small to large. In contrast, there is also a prev_permutation function;
The prev_permutation () function outputs all the smaller orders than the current one. The order is from large to small.
If you are not familiar with it, you can compile a test function to test it;
# Include <iostream> # include <cstring> # include <algorithm> using namespace STD; int main () {// int A [] = {3, 1, 2 }; int A [] = {1, 2, 3 }; do {cout <A [0] <"" <A [1] <"" <A [2] <Endl;} while (next_permutation (, A + 3); // A + 3 is the size of the array // while (prev_permutation (A, A + 3); Return 0 ;}
You can test the results of the two groups of data;
The first group of {3, 1, 2} uses next to get the results of {3, 1, 2} and {3, 2, 1 };
The result obtained by Pre is {, 2}, {, 1}, {, 3}, {, 2,}, {, 3 };
In the second group {, 3}, the result obtained with next is {, 3}, {, 2}, {, 3}, {, 1, 2 },{ 3, 2, 1 };
The result obtained by Pre is {1, 2, 3 };
We can see from the above conclusion: to get a full arrangement, we need to sort the given array; the next function defaults to the order from small to large, the default order of the PRE function is from large to small;
For details about the principles of these two functions, refer to these two blogs;
Http://leonard1853.iteye.com/blog/1450085 http://www.cnblogs.com/mycapple/archive/2012/08/13/2635853.html
Let's take a look at the code for the above question:
# Include <cstdio> # include <cstring> # include <algorithm> char a [10] = {'1', '2', '3', '4 ', '5', '6', '7', '8', '9', '\ 0'}; // Array Using namespace STD with a given size sequence number; int main () {int t, n, m; char B [10], C [10]; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); strcpy (B, A); // The full sorting starts in the lexicographically ascending order, therefore, the first group of data is copied directly from small to large. B [m] = '\ 0'; // copy the previous m printf ("% s \ n", B ); while (next_permutation (A, A + n) // arrange the array in full {strcpy (C, A); C [m] = '\ 0 '; if (strcmp (B, c) // judge whether arrays B and C are equal {strcpy (B, c ); // output B [m] = '\ 0'; printf ("% s \ n", B) ;}} return 0 ;}
The following is written recursively. The main idea is search + backtracking;
Judge from 1 to see if the number is already in the sequence, and then judge the next one;
Understand the concept of recursion;
The following code is used:
# Include <cstdio> # include <cstring> int visit [20], a [20]; // mark the array int n, m; void DFS (int pos) {If (Pos = m) // indicates the end of recursion {for (INT I = 0; I <m; I ++) printf ("% d ", A [I]); // outputs printf ("\ n"); return;} For (INT I = 1; I <= N; I ++) {If (! Visit [I]) // This is not accessed {visit [I] = 1; // it indicates that a [POS] = I has been accessed; // The first number of this sequence DFS (Pos + 1); // search for the next number visit [I] = 0; // backtracking }}int main () {int T; scanf ("% d", & T); While (t --) {memset (visit, 0, sizeof (visit )); // initialize scanf ("% d", & N, & M); DFS (0);} return 0 ;}