forked from ekg/multichoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultichoose.c
53 lines (43 loc) · 945 Bytes
/
multichoose.c
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
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv){
int *a, *b;
char **m;
int i,j,j_1,k,n,r;
if (argc<3) {
printf("usage: ./multi_erik k item_1 ... item_n\n");
return 0;
}
n = atoi(argv[1]);
r = argc - 2;
m = malloc(r * sizeof(char*));
a = malloc(n * sizeof(int));
b = malloc(n * sizeof(int));
for (i=2;i<argc;i++)
m[i-1] = argv[i];
for (i=1;i<=n;i++) {
a[i] = 1; b[i] = r;
}
j=n;
while(1){
// emit multiset combination
for(i=1;i<=n;i++)
printf("%s ", m[a[i]]);
printf("\n");
j=n;
while(a[j]==b[j])j--;
if (j<=0) break;
j_1=j;
while(j_1<=n){
a[j_1]=a[j_1]+1;
k=j_1;
while(k<n) {
a[k+1]=a[k];
k++;
}
k++;
j_1=k;
}
}
return 0;
}