Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int n = 3;
  5. int used[n + 1];
  6. int a[n];
  7.  
  8.  
  9. void print() {
  10.     for (int i = 0; i < n; ++i) {
  11.         cout << a[i] << ' ';
  12.     }
  13.     cout << '\n';
  14. }
  15.  
  16. void rec(int idx) {
  17.     for (int i = 1; i <= n; ++i) {
  18.         if (!used[i]) {
  19.             used[i] = true;
  20.             a[idx] = i;
  21.             if (idx == n - 1) {
  22.                 print();
  23.             }
  24.             else {
  25.                 rec(idx + 1);
  26.             }
  27.             used[i] = false;
  28.         }
  29.     }
  30. }
  31. int main() {
  32.     for (int i = 1; i <= n; ++i) {
  33.         used[i] = false;
  34.     }
  35.     rec(0);
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement