-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob24.cpp
92 lines (91 loc) · 2.08 KB
/
prob24.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
int permute(int *n,int m,int level,int *count);
int printPer(int *n,int m,int *count,int level);
int main(){
int n[10];
int i,m;
scanf("%d",&m);
for(i=0;i<=m-1;i++){
n[i]=i;
}
int count=0;
permute(n,m,1,&count);
printf("\n");
}
int printPer(int *n,int m,int *count,int level){
(*count)++;
//printf("level = %d %d >> \t{ ",level,*count);
printf("%d >> \t{ ",*count);
for(int i=0;i<m;i++){
printf(" %d ",n[i]);
}
printf(" }");
return 0;
}
int permute(int *n,int m,int level,int *count){
int xcount=0;
//printf("\nlevel = %d",level);
//printf("Count = %d ",*count);
if(level==m){
if(*(count)==999999){
printf("\n");
// for(int k=0;k<level;k++){
// printf("*");
// }
printPer(n,m,count,level);
}else{
(*count)++;
}
return 0;
}
int i,j,t,x;
// for(i=0;i<m-level;i++){
// hold[i]=n[level+i];
// }
for(i=0;i<=m-level;i++){
// for(j=0;j<=m-level-1;j++){
// n[level+j]=hold[j];
// }
permute(n,m,level+1,count);
if((level+i)>=m){
break;
}
//reverse the rest except the 1st element
//if(level==1){
// printf("\nBefore revering");
// for(int k=0;k<level;k++){
// printf("*");
// }
// printPer(n,m,&xcount,level);
//}
j=level;
while(1){
t=m-j+level-1;
if(j>t){
break;
}
x=n[j];
n[j]=n[t];
n[t]=x;
j++;
}
//if(level==1){
// printf("\nAfter Reversing");
// for(int k=0;k<level;k++){
// printf("*");
// }
// printPer(n,m,&xcount,level);
//}
//after reversing exchange the next 2 elements as described
//it can be showen that after that all the elements starting from level ... m-1["current" element is at level-1]
//will be lexicographially smallest ,.. so each time after this we are in a same situation where we need to permute
//a set of less than size 1 and given in "decreasing to increasing order" ... and as the changes actually does not depend
//on the values of n[i] so if initially they are in dec to inc then the recursion invariant holds !!!!!!!!!
//24-01-2013
t=n[level-1];
n[level-1]=n[level+i];
n[level+i]=t;
}
return 0;
}