If the search depth is deep or the depth is not fixed, you cannot use the enumeration method to set the layers of nested loops. In this case, you can use recursion to complete the search task. Recursion is a common algorithm, and it is another Implementation Method of search. If a function or process is used in algorithm design to directly or indirectly call itself to solve the problem, this method is called a recursive algorithm. A recursive algorithm must have one or more definite recursive termination conditions.
Sample Input
3
1 2 3
Sample output
123
132
213
231
312
321
# Include <stdio. h> # include <string. h> const int maxn = 11; int N; int used [maxn]; // mark the array int mat [maxn]; // store the array int num [maxn]; // output array void solve (int l) {If (L> = N) {for (INT I = 0; I <n; ++ I) printf ("% d", num [I]); puts (""); Return ;}for (INT I = 0; I <n; ++ I) {If (! Used [I]) {used [I] = 1; num [l] = mat [I]; solve (L + 1); used [I] = 0 ;}}} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 0; I <n; ++ I) scanf ("% d", mat + I); memset (used, 0, sizeof (used); solve (0);} return 0 ;}