Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中生成集合的子集(非递归)_C_Set_Subset - Fatal编程技术网

在C中生成集合的子集(非递归)

在C中生成集合的子集(非递归),c,set,subset,C,Set,Subset,我试图以非递归的方式生成集合的所有子集。代码如下: #include <stdio.h> main() { int no_of_element, no_of_subset, i, j, start, index, a[50], x; printf("Enter the size of main set :"); scanf("%d", &no_of_element); printf("Enter the elements of main se

我试图以非递归的方式生成集合的所有子集。代码如下:

#include <stdio.h>

main() {
    int no_of_element, no_of_subset, i, j, start, index, a[50], x;
    printf("Enter the size of main set :");
    scanf("%d", &no_of_element);
    printf("Enter the elements of main set :");
    for (x = 0; x < no_of_element; x++)
        scanf("%d", &a[x]);
    printf("The subsets are :\n");

    for (no_of_subset = 1; no_of_subset <= no_of_element; no_of_subset++) {
        for (start = 0; start <= no_of_element - no_of_subset; start++) {
            if (no_of_subset == 1)
                printf("%d\n", a[start]);
            else {
                index = start + no_of_subset - 1;
                for (j = index; j < no_of_element; j++) {
                    for (i = start; i < index; i++) {
                        printf("%d\t", a[i]);
                    }
                    printf("%d\n", a[j]);
                }
            }
        }
    }
}

此代码没有生成子集1,3,4。有人能帮我修一下吗?

只是为了好玩,我从头开始编辑。 诀窍是让计数器遍历所有可能的子集,每个子集都是其位的组合

在这种情况下,请使用unsigned long int来处理最多64个元素的集合:

#include <stdio.h>

void print_subset(int subset, int *set) {
    int pos=0;
    printf("Subset %d = ", subset);
    while (subset) {
        if (subset & 1) {
            printf("%d ", set[pos]);
        }
        subset >>= 1;
        pos++;
    }
    printf("\n");
}

int main()
{
 int no_of_element,no_of_subset,x,a[50];

 printf("Enter the size of main set :");
     scanf("%d",&no_of_element);
 printf("Enter the elements of main set :");
 for(x=0;x<no_of_element;x++)
     scanf("%d",&a[x]);

 no_of_subset= (1 << no_of_element);
 printf("There are %d subsets\n", no_of_subset);

 for (; no_of_subset--;) {
     print_subset(no_of_subset, a);
 }
}

您目前的输入和输出是什么?更新了。输入是:4个元素1、2、3和4年前,我实际上为一个类似的问题想出了一个聪明的解决方案,即找出适合给定长度磁带的最佳音轨组合。使用n位计数器,即最大值为2^n。计数器的每个第k位表示集合中第k个元素的存在1/不存在0。请将输出作为文本,而不是外部图像发布。我认为就复杂性而言,您无法获得更好的算法
#include <stdio.h>

void print_subset(int subset, int *set) {
    int pos=0;
    printf("Subset %d = ", subset);
    while (subset) {
        if (subset & 1) {
            printf("%d ", set[pos]);
        }
        subset >>= 1;
        pos++;
    }
    printf("\n");
}

int main()
{
 int no_of_element,no_of_subset,x,a[50];

 printf("Enter the size of main set :");
     scanf("%d",&no_of_element);
 printf("Enter the elements of main set :");
 for(x=0;x<no_of_element;x++)
     scanf("%d",&a[x]);

 no_of_subset= (1 << no_of_element);
 printf("There are %d subsets\n", no_of_subset);

 for (; no_of_subset--;) {
     print_subset(no_of_subset, a);
 }
}
Enter the size of main set :4
Enter the elements of main set :1
2
3
4
There are 16 subsets
Subset 15 = 1 2 3 4 
Subset 14 = 2 3 4 
Subset 13 = 1 3 4 
Subset 12 = 3 4 
Subset 11 = 1 2 4 
Subset 10 = 2 4 
Subset 9 = 1 4 
Subset 8 = 4 
Subset 7 = 1 2 3 
Subset 6 = 2 3 
Subset 5 = 1 3 
Subset 4 = 3 
Subset 3 = 1 2 
Subset 2 = 2 
Subset 1 = 1 
Subset 0 = 
#include <stdio.h>
#include <stdlib.h>

void print(size_t n, int a[n]){
    size_t i;

    for(i=0; i < n; ++i){
        if(i)
            putchar(',');
        printf("%d", a[i]);
    }
    putchar('\n');
}

void subset(size_t n, int a[n], size_t size){
    int *out = malloc(size * sizeof(*out));
    int *level = malloc((size+1)*sizeof(int));
    int *index = malloc((size+1)*sizeof(int));
    int sp = 0;

    level[sp] = 0;
    index[sp] = 0;
redo:
    while(index[sp] < n){
        out[level[sp]] = a[index[sp]];
        level[sp+1] = level[sp] + 1;
        index[sp+1] = index[sp] + 1;
        if(level[++sp] == size){
            print(size, out);
            --sp;
        } else 
            goto redo;//this means recursive call
        ++index[sp];
    }
    if(sp>0){
        ++index[--sp];
        goto redo;
    }
    free(level);
    free(index);
    free(out);
}

int main(void){
    int a[] = {1, 2, 3, 4};
    size_t i, n = sizeof(a)/sizeof(*a);
    for(i = 1; i <= n; ++i)
        subset(n, a, i);

    return 0;
}
1
2
3
4
1,2
1,3
1,4
2,3
2,4
3,4
1,2,3
1,2,4
1,3,4
2,3,4
1,2,3,4