Georgiy031

Untitled

Apr 14th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <numeric>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int fac(int a) { return (a ? a * fac(a - 1) : 1); }
  8.  
  9. bool next_perm(vector<int>& v) {
  10.     if (v.size() <= 1) return false;
  11.     int i = v.size() - 1;
  12.     while (true) {
  13.         int ind1 = i;
  14.         int ind2;
  15.         if (v[--i] < v[ind1]) {
  16.             ind2 = v.size();
  17.             while (v[i] >= v[--ind2]);
  18.             swap(v[i], v[ind2]);
  19.             reverse(v.begin() + ind1, v.end());
  20.             return true;
  21.         }
  22.         if (i == 0) {
  23.             reverse(v.begin(), v.end());
  24.             return false;
  25.         }
  26.     }
  27. }
  28.  
  29. int main() {
  30.     setlocale(LC_ALL, "Russian");
  31.     int n;
  32.     cout << "Введите размер перестановки: ";
  33.     cin >> n;
  34.     vector<int> v(n);
  35.     iota(v.begin(), v.end(), 1);
  36.  
  37.     for (auto& x : v) cout << x << " ";
  38.     cout << "\n";
  39.  
  40.     while (next_perm(v)) {
  41.         for (auto& x : v) cout << x << " ";
  42.         cout << "\n";
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment