-
Notifications
You must be signed in to change notification settings - Fork 0
/
p39_permutation.cpp
58 lines (46 loc) · 1022 Bytes
/
p39_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
#include <iostream>
#include <algorithm>
const int MAX_N = 1000000;
bool used[MAX_N];
int perm[MAX_N];
int N;
// get n! pattern
void permutation1(int pos, int n){
if (pos == n){
// processing perm
for(int j=0; j<n; j++) std::cout << perm[j];
std::cout << '\n';
return ;
}
// loop to pick up pos of perm from 0...n-1
for (int i=0; i<n; i++){
if(!used[i]){
perm[pos] = i;
used[i] = true;
permutation1(pos + 1, n);
used[i] = false;
}
}
return ;
}
int perm2[MAX_N];
void permutation2(int n){
for(int i=0; i<n; i++){
perm2[i] = i;
}
do {
// process perm2
for(int j=0; j<n; j++) std::cout << perm2[j];
std::cout << '\n';
} while (std::next_permutation(perm2, perm2+n));
return ;
}
int main(){
std::cout << "Input N :";
std::cin >> N;
for(int i=0; i<N; i++) used[i]=false;
std::cout << "------------------Recursive Function----------------" << '\n';
permutation1(0,N);
std::cout << "------------------------Library---------------------" << '\n';
permutation2(N);
}