Advertisement
STANAANDREY

perm cls

Oct 20th, 2020
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int st[20], n;
  4.  
  5. void tipar(int k) {
  6.     for (int i = 1; i <= k; i++)
  7.         cout << st[i] << ' ';
  8.     cout << endl;
  9. }
  10.  
  11. int valid(int k) {
  12.     for (int i = 1; i < k; i++)
  13.         if (st[i] == st[k])
  14.             return 0;
  15.     return 1;
  16. }
  17.  
  18. int sol(int k) {
  19.     return k == n;
  20. }
  21.  
  22. void bktr() {
  23.     int k = 1;
  24.     st[k] = 0;
  25.     while (k) {
  26.         if (st[k] < n) {
  27.             st[k]++;
  28.             if (valid(k)) {
  29.                 if (sol(k))
  30.                     tipar(k);
  31.                 else {
  32.                     k++;
  33.                     st[k] = 0;
  34.                 }
  35.             }
  36.         }
  37.         else
  38.             k--;
  39.     }
  40. }
  41.  
  42. int main() {
  43.     cin >> n;
  44.     bktr();
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement