#include<stdio.h>
int memo[6][10];
void display(int *sub,int n)
{
int i=0;
while(i<n)
{
printf("%d\n",sub[i]);
i++;
}
printf("\n end\n");
}
void subset_sum(int *a,int *sub,int sum,int n)
{
static int i=0;
if(sum==0)
{
display(sub,i);
return ;
}
if(n==0 && sum!=0)
{
return ;
}
if(a[n-1]>sum)
subset_sum(a,sub,sum,n-1);
sub[i]=a[n-1];
++i;
subset_sum(a,sub,sum-a[n-1],n-1);
i--;
subset_sum(a,sub,sum,n-1);
}
main()
{
int a[]={3,34,4,12,5,2,5,6};
int sub[10];
int sum=6;
int i=0;
int j=0;
for(;i<6;i++)
{
for(;j<20;j++)
{
memo[i][j]=0;
}
}
subset_sum(a,sub,sum,8);
getch();
}
int memo[6][10];
void display(int *sub,int n)
{
int i=0;
while(i<n)
{
printf("%d\n",sub[i]);
i++;
}
printf("\n end\n");
}
void subset_sum(int *a,int *sub,int sum,int n)
{
static int i=0;
if(sum==0)
{
display(sub,i);
return ;
}
if(n==0 && sum!=0)
{
return ;
}
if(a[n-1]>sum)
subset_sum(a,sub,sum,n-1);
sub[i]=a[n-1];
++i;
subset_sum(a,sub,sum-a[n-1],n-1);
i--;
subset_sum(a,sub,sum,n-1);
}
main()
{
int a[]={3,34,4,12,5,2,5,6};
int sub[10];
int sum=6;
int i=0;
int j=0;
for(;i<6;i++)
{
for(;j<20;j++)
{
memo[i][j]=0;
}
}
subset_sum(a,sub,sum,8);
getch();
}
No comments:
Post a Comment
Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.