-
Notifications
You must be signed in to change notification settings - Fork 0
/
10974.cpp
42 lines (41 loc) · 818 Bytes
/
10974.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#pragma warning(disable : 4996)
using namespace std;
bool anext_permutation(vector<int> &a)
{
int i = a.size() - 1;
while (i > 0 && a[i - 1] >= a[i])
i--;
if (i <= 0)
return false;
int j = a.size() - 1;
while (a[j] <= a[i - 1])
j--;
int temp = a[j];
a[j] = a[i - 1];
a[i - 1] = temp;
j = a.size() - 1;
reverse(a.begin() + i, a.end());
return true;
}
int main(void)
{
int n;
cin >> n;
vector<int> x;
for (int i = 1; i <= n; i++)
{
x.push_back(i);
}
do
{
for (vector<int>::iterator it = x.begin(); it != x.end(); it++)
{
printf("%d ", *it);
}
puts("");
} while (anext_permutation(x));
}