1. Recursive enumeration
1#include <iostream>2 using namespacestd;3 4 voidPrint_permutation (intNint*p,intcur)5 {6 if(cur = =N)7 {8 for(inti =0; I < n; i++)9cout <<P[i];Tencout <<Endl; One } A Else for(inti =1; I <= N; i++) - { - intOK =1; the for(intj =0; J < cur; J + +) - if(P[j] = = i) OK =0; - if(OK) - { +P[cur] =i; -Print_permutation (n, p, cur +1); + } A } at } - int_tmain (intARGC, _tchar*argv[]) - { - intn,p[10000],cur=0; -CIN >>N; - print_permutation (n, p, cur); in return 0; -}
2, Stl--next_permutation
1#include <iostream>2#include <algorithm>3 using namespacestd;4 5 int_tmain (intARGC, _tchar*argv[])6 {7 intN, p[Ten];8CIN >>N;9 for(inti =0; I < n; i++)TenCIN >>P[i]; OneSort (p, p +n); A Do{ - for(inti =0; I < n; i++) -cout <<P[i]; thecout <<Endl; -} while(Next_permutation (p, p +n)); - return 0; -}
Two common ways to enumerate permutations