-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
46 lines (41 loc) · 845 Bytes
/
Permutations.cpp
File metadata and controls
46 lines (41 loc) · 845 Bytes
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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void permute(vector<int>& nums)
{
vector<vector<int>> v;
vector<int> a;
int size = nums.size();
if(size==1)
{
v.push_back(nums);
cout << v[0][0];
exit(1);
}
sort(nums.begin(), nums.end());
do{
for(int i=0; i<size; i++)
{
a.push_back(nums[i]);
}
v.push_back(a);
a.clear();
}while(next_permutation(nums.begin(), nums.end()));
cout << "Output: \n";
for(int i=0; i<v.size(); i++)
{
for(int j=0; j<v[i].size(); j++)
{
cout << v[i][j] << " ";
}
cout <<"\n";
}
}
int main()
{
vector<int> v(3);
for(int i=0; i<v.size(); i++)
cin >> v[i];
permute(v);
}