Skip to content

Instantly share code, notes, and snippets.

@gallirohik
Created April 16, 2018 17:10
Show Gist options
  • Select an option

  • Save gallirohik/7f09faf5a5c8e8b1addf4fdd60f8de20 to your computer and use it in GitHub Desktop.

Select an option

Save gallirohik/7f09faf5a5c8e8b1addf4fdd60f8de20 to your computer and use it in GitHub Desktop.
generate subsets
#include <stdio.h>
#include <stdlib.h>
int add(int *a,int n)
{
int i;
i=n-1;
while(i>=0)
{
if(a[i]&1)
{
a[i]=0;
i--;
}
else
{
a[i]=1;
return 1;
}
}
return 0;
}
void subset(int *a,int *arr,int n)
{
int i;
printf("{ Null,\n");
do{
for(i=0;i<n;i++)
if(a[i])
printf("%d ",arr[i]);
printf(",");
}while(add(a,n));
printf("}");
}
int main()
{
int n,i,*a,*arr;
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
a=(int*)calloc(n,sizeof(int));
a[n-1]=1;
subset(a,arr,n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment