-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutation.cpp
77 lines (63 loc) · 1.17 KB
/
Permutation.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void nextPermutation(vector<int> &num) {
for ( size_t i = num.size()-1; i >0; i--)
{
if (num[i - 1] < num[i]) {
//if (i= num.size() - 1) swap the last two
//{
// int tmp = num[i];
// num[i] = num[i - 1];
// num[i - 1] = tmp;
//}
for (size_t j = num.size()-1; j >=i ; j--)
{
if (num[i - 1] < num[j]) {
int tmp = num[j];
num[j] = num[i - 1];
num[i - 1] = tmp;
reverse(num.begin() + i, num.end());
return;
}
}
}
if (i==1)
{
reverse(num.begin(), num.end());
return;
}
}
}
bool isBeginVector(vector<int> &num) {
for (int i = 0; i < num.size(); i++) {
if (num[i] != i + 1) {
return false;
}
}
return true;
}
void printVector(vector<int> &num) {
for (int i = 0; i < num.size(); i++) {
cout << num[i] << " ";
}
cout << endl;
}
int main(int argc, char** argv) {
unsigned int n=4;
if (argc > 1)
n = atoi(argv[1]);
vector<int> num;
for (size_t i = 1; i <=n; i++)
{
num.push_back(i);
}
while (true) {
printVector(num);
nextPermutation(num);
if (isBeginVector(num))
break;
}
return 0;
}