Advertisement
STANAANDREY

perm dans

Oct 19th, 2020
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include<iostream>
  2. #define NMAX 20
  3. using namespace std;
  4. int n, c1, c2, st[NMAX], used[NMAX];
  5.  
  6. void display() {
  7.     for (int i = 1; i <= n; i++)
  8.         cout << st[i] << ' ';
  9.     cout << endl;
  10. }
  11.  
  12. void bktr(int k) {
  13.     if (k == n + 1)
  14.         display();
  15.     else if (k != 2 && k != n - 1) {
  16.         for (int i = 1; i <= n; i++)
  17.             if (!used[i]) {
  18.                 used[i] = 1;
  19.                 st[k] = i;
  20.                 bktr(k + 1);
  21.                 used[i] = 0;
  22.             }
  23.     } else
  24.         bktr(k + 1);
  25. }
  26.  
  27. int main () {
  28.     cin >> n >> c1 >> c2;
  29.     st[2] = c1, st[n - 1] = c2;
  30.     used[c1] = used[c2] = 1;
  31.     bktr(1);
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement