Heavy: If the problem turns into an input array p and outputs all the full permutations of the array a elements in the dictionary order, you need to modify the full arrangement of the code set
//Rujia Liu#include <cstdio>#include<algorithm>using namespacestd;intp[ -], a[ -];//the full arrangement of elements in the output array p. There may be duplicate elements in the array PvoidPrint_permutation (intNintPintAintcur) { if(cur = =N) { for(inti =0; I < n; i++) printf ("%d", A[i]); printf ("\ n");
printf ("cur=%d\n", cur); } Else for(inti =0; I < n; i++) //P[i] = = P[i-1], then p[i] This number only consider p[i-1] on the line//i = = 0 o'clock so be sure to take it because there is no more before it can be considered if(!i | | P[i]! = p[i-1]) { intC1 =0, C2 =0; //A is already filled in, p is not filled in//use C1 c2 statistics have been filled in the number and all, if C1 < C2 description has not filled in for(intj =0; J < cur; J + +) if(A[j] = = P[i]) c1++; for(intj =0; J < N; J + +) if(P[i] = = P[j]) c2++;//printf ("i=%d cur=%d c1=%d c2=%d\n", I, CUR,C1,C2); if(C1 <C2) {A[cur] =P[i]; Print_permutation (n, P, A, cur+1); } }}intMain () {intI, N; scanf ("%d", &N); for(i =0; I < n; i++) scanf ("%d", &P[i]); Sort (p, p+N); Print_permutation (n, P, A,0); return 0;}
Test
Introduction to the algorithm Classic-seventh chapter example 7-2-2 the permutation of the re-set