Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <numeric>
- #include <algorithm>
- using namespace std;
- int fac(int a) { return (a ? a * fac(a - 1) : 1); }
- bool next_perm(vector<int>& v) {
- if (v.size() <= 1) return false;
- int i = v.size() - 1;
- while (true) {
- int ind1 = i;
- int ind2;
- if (v[--i] < v[ind1]) {
- ind2 = v.size();
- while (v[i] >= v[--ind2]);
- swap(v[i], v[ind2]);
- reverse(v.begin() + ind1, v.end());
- return true;
- }
- if (i == 0) {
- reverse(v.begin(), v.end());
- return false;
- }
- }
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- int n;
- cout << "Введите размер перестановки: ";
- cin >> n;
- vector<int> v(n);
- iota(v.begin(), v.end(), 1);
- for (auto& x : v) cout << x << " ";
- cout << "\n";
- while (next_perm(v)) {
- for (auto& x : v) cout << x << " ";
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment