-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.cpp
More file actions
60 lines (51 loc) · 993 Bytes
/
Copy pathpermutation.cpp
File metadata and controls
60 lines (51 loc) · 993 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string.h>
#include <utility>
using namespace std;
void permutation(char * str, int len, int n){
if ( n == len -1 ) {
cout<< str << endl;
return;
}
for (int i = n; i < len; ++i)
{
swap(str[i], str[n]);
permutation(str, len, n + 1);
swap(str[i], str[n]);
}
}
void reverse(int * start, int * end){
while(start < end){
int tmp = * start;
*start ++ = *end;
*end -- = tmp;
}
}
bool next_permutation(int * a, int size){
int i = size -2;
while((i >= 0) && (a[i] > a[i + 1])) i --;
if (i < 0)
return false;
int j = size -1 ;
while(a[j] < a[i]) j --;
swap(a[i], a[j]);
reverse(a + i + 1, a + size - 1);
return true;
}
void permutation_unrecursion(){
int a[] = {1, 2, 3, 4};
while(next_permutation(a, 4)){
for (int i = 0; i < 4; ++i)
{
cout << a[i];
}
cout << endl;
}
}
int main(int argc, char const *argv[])
{
// char str[] = "abcd";
// permutation(str, strlen(str), 0);
permutation_unrecursion();
return 0;
}