fork download
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4.  
  5. int n, a[10], perm[10], used[10], d = 1;
  6. set<string> seen;
  7.  
  8. void pr() {
  9. string state = "";
  10. for (int i = 0; i < n; i++) {
  11. state += to_string(perm[i]) + " ";
  12. }
  13.  
  14. if (seen.find(state) == seen.end()) {
  15. cout << d << ": " << state << endl;
  16. seen.insert(state);
  17. d++;
  18. }
  19. }
  20.  
  21. void permutation(int i) {
  22. for (int j = 0; j < n; j++) {
  23. if (!used[j]) {
  24. perm[i] = a[j];
  25. used[j] = 1;
  26. if (i == n - 1)
  27. pr();
  28. else
  29. permutation(i + 1);
  30. used[j] = 0;
  31. }
  32. }
  33. }
  34.  
  35. int main() {
  36. cin >> n;
  37. for (int i = 0; i < n; i++) {
  38. cin >> a[i];
  39. }
  40. permutation(0);
  41. }
  42.  
Success #stdin #stdout 0s 5272KB
stdin
5 
1 2 2 3 4
stdout
1: 1 2 2 3 4 
2: 1 2 2 4 3 
3: 1 2 3 2 4 
4: 1 2 3 4 2 
5: 1 2 4 2 3 
6: 1 2 4 3 2 
7: 1 3 2 2 4 
8: 1 3 2 4 2 
9: 1 3 4 2 2 
10: 1 4 2 2 3 
11: 1 4 2 3 2 
12: 1 4 3 2 2 
13: 2 1 2 3 4 
14: 2 1 2 4 3 
15: 2 1 3 2 4 
16: 2 1 3 4 2 
17: 2 1 4 2 3 
18: 2 1 4 3 2 
19: 2 2 1 3 4 
20: 2 2 1 4 3 
21: 2 2 3 1 4 
22: 2 2 3 4 1 
23: 2 2 4 1 3 
24: 2 2 4 3 1 
25: 2 3 1 2 4 
26: 2 3 1 4 2 
27: 2 3 2 1 4 
28: 2 3 2 4 1 
29: 2 3 4 1 2 
30: 2 3 4 2 1 
31: 2 4 1 2 3 
32: 2 4 1 3 2 
33: 2 4 2 1 3 
34: 2 4 2 3 1 
35: 2 4 3 1 2 
36: 2 4 3 2 1 
37: 3 1 2 2 4 
38: 3 1 2 4 2 
39: 3 1 4 2 2 
40: 3 2 1 2 4 
41: 3 2 1 4 2 
42: 3 2 2 1 4 
43: 3 2 2 4 1 
44: 3 2 4 1 2 
45: 3 2 4 2 1 
46: 3 4 1 2 2 
47: 3 4 2 1 2 
48: 3 4 2 2 1 
49: 4 1 2 2 3 
50: 4 1 2 3 2 
51: 4 1 3 2 2 
52: 4 2 1 2 3 
53: 4 2 1 3 2 
54: 4 2 2 1 3 
55: 4 2 2 3 1 
56: 4 2 3 1 2 
57: 4 2 3 2 1 
58: 4 3 1 2 2 
59: 4 3 2 1 2 
60: 4 3 2 2 1