编程知识 cdmana.com

Find all subsets (arrays) of the set to run locally. There is no problem with the answer, but the error is always displayed on OJ!

Title Description

The number of an element is n(n>0) Set , Yes 2n A subset of ( Including empty sets ). for example , aggregate A={a,b,c} The subset of is 23=8 individual , Respectively :{}( An empty set )、{a}、{b}、{a、b}、{c}、{a,c},{b,c},{a,b,c}.

Enter a set ( Assume that the set element is a character ), Give all subsets of the set . The output order of the subset is obtained from the sample output analysis .

Input

Number of tests t
The format of each group of test data is : Number of collection elements n, Followed by a collection of n Elements .

Output

For each group of test data , Number of output subsets , A subset of each subsequent line , Output all subsets .

sample input
2
3
a b c
4
e f a d

sample output

8{}{a}{b}{a,b}{c}{a,c}{b,c}{a,b,c}16{}{e}{f}{e,f}{a}{e,a}{f,a}{e,f,a}{d}{e,d}{f,d}{e,f,d}{a,d}{e,a,d}{f,a,d}{e,f,a,d}

My code

# include<stdio.h># include<math.h># include<memory.h>int main(){    int t,n,i,count,j,m,k,q,p; //count Is the number of record subsets      scanf("%d",&t);    for(q=1;q<=t;q++)    {        count=0;        scanf("%d",&n);        getchar();        int a[n];        memset(a,0,n*sizeof(int));        char str[n];        for(i=0;i<n;i++)        {        scanf("%c",&str[i]);        getchar();        }        count=pow(2,n);        printf("%d\n",count);        printf("{}\n"); // First empty set ;         for(j=1;j<count;j++)        {            m=j;            k=0;            while(m!=0)            {            a[k]=m%2;            m=m/2;            k++;                }                printf("{");            for(i=0;i<n;i++)            {                if(a[i]==1)                {                    printf("%c,",str[i]);                    }            }            printf("\b}\n");                                        }        if(q!=t)        {        printf("\n");        }                    }}

Local run results

img


So what's wrong Ask for an answer !

版权声明
本文为[CSDN Q & A]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/12/20211207180851610O.html

Scroll to Top