File: multichoose.c

package info (click to toggle)
libvcflib 1.0.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 49,628 kB
  • sloc: cpp: 39,244; perl: 474; python: 329; ruby: 285; sh: 247; ansic: 198; makefile: 131; javascript: 94; lisp: 57
file content (53 lines) | stat: -rw-r--r-- 945 bytes parent folder | download | duplicates (7)
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
#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;
}