(1) generate 1 ~ N Arrangement
Analysis: solve the problem with recursion: first output all the arrays starting with 1 (this step is called recursively), and then output the arrangement starting with 2 (it is also called recursively ), next we start with 3 ...... the last is the arrangement starting with N.
Pseudocode:
Void print_permutation (sequence a, set S ){
If (S is empty) output sequence;
Else considers each element V {
Print_permutation (add V to the end of a to obtain the new sequence, S-{v });
}
}
The following is the implementation code:
# Include <iostream> using namespace STD; const int maxn = 1000; int A [maxn], n; void print_permutation (int n, int * a, int cur) {If (cur = N) {// recursive boundary for (INT I = 0; I <n; ++ I) cout <A [I] <""; cout <Endl;} else {for (INT I = 1; I <= N; ++ I) {// try to fill in various integers in a [cur] iint OK = 1; for (Int J = 0; j <cur; ++ J) {if (a [J] = I) OK = 0; // if I already exists in a [0] ~ If a [cur-1] appears, you cannot select another.} If (OK) {// I is in a [0] ~ A [cur-1] <span style = "font-family: Arial, Helvetica, sans-serif;">, therefore, you can put I into a [cur] </span> A [cur] = I; print_permutation (n, A, cur + 1 ); // recursive call }}int main () {CIN> N; print_permutation (n, A, 0); Return 0 ;}
(2) generate the arrangement of distinct sets
Input array P and output all elements of array P in Lexicographic Order.
# Include <iostream> # include <algorithm> using namespace STD; const int maxn = 1000; int A [maxn], p [maxn], n; void print_permutation (int n, int * P, int * a, int cur) {If (cur = N) {for (INT I = 0; I <n; ++ I) cout <A [I] <"; cout <Endl;} else for (INT I = 0; I <n; ++ I) if (! I | P [I]! = P [I-1]) {// If condition makes sure that the same element is not recursively called repeatedly, this ensures that the subscript I we enumerate should be retrieved all P [I] values int C1 = 0, C2 = 0; For (Int J = 0; j <cur; ++ J) if (P [I] = A [J]) C1 ++; // A [0] ~ The number of occurrences of P [I] in a [cur-1] is placed in C1 for (Int J = 0; j <n; ++ J) if (P [I] = P [J]) C2 ++; // The occurrences of P [I] in the p array are placed in C2. If (C1 <C2) {// As long as C1 <C2 can be called recursively, this ensures that the same elements are not missed. A [cur] = P [I]; print_permutation (N, P, A, cur + 1) ;}} int main () {CIN> N; for (INT I = 0; I <n; ++ I) CIN> P [I]; sort (p, p + n); print_permutation (N, P,, 0); Return 0 ;}
(3) generate the next arrangement:
Another way to enumerate all sorts is to constantly call the process of "finding the next sort" starting from the smallest arrangement in the Lexicographic Order.
Use next_permutation in STL to find the next arrangement,
# Include <iostream> # include <cstdio> # include <algorithm> # define maxn 1000 using namespace STD; int P [maxn]; int main () {int N; cin> N; For (INT I = 0; I <n; ++ I) {CIN> P [I];} Sort (p, p + n ); // sort and get the smallest do {for (INT I = 0; I <n; ++ I) {cout <p [I] <"";} cout <Endl;} while (next_permutation (p, p + n); Return 0 ;}