-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations.cpp
48 lines (39 loc) · 1.19 KB
/
permutations.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
using namespace std;
void printVector(vector<int>& vec)
{
for_each(vec.begin(), vec.end(), [](int &ele){ cout << ele << " ";});
cout << endl;
}
int main()
{
vector<int> vec = {9,6,2,4,3,8,7,5,1};
printVector(vec);
std::rotate(vec.begin(), vec.begin() + 3, vec.end()); //13
//std::rotate(vec.begin() + 1, vec.begin() + 3, vec.begin() + 6);
printVector(vec);
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(vec.begin(), vec.end(), std::default_random_engine(seed)); //14
printVector(vec);
cout << "===========" << endl;
std::vector<int> vt = {1, 2, 3};
do {
printVector(vt);
} while ( std::next_permutation(vt.begin(), vt.end()) ); //15
cout << "===========" << endl;
printVector(vt);
cout << "===========" << endl;
std::vector<int> vt2 = {3, 2, 1};
do {
printVector(vt2);
} while ( std::prev_permutation(vt2.begin(), vt2.end()) ); //16
cout << "===========" << endl;
printVector(vt2);
std::reverse(vec.begin(), vec.end()); //17
printVector(vec);
return 0;
}