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
|
#include<stdio.h>
int count,w[10],d,x[10];
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if((cs+w[k])>d)
{
printf("\n Subset solution = %d\n",++count);
for(i=0; i<=k; i++)
{
if(x[i]==1)
printf("%d\n",w[i]);
}
}
else if(cs+w[k]+w[k+1] <=d)
subset(cs+w[k],k+1,r-w[k]);
if((cs+r-w[k]>=d)&&(cs+w[k+1])<=d)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
int main()
{
int sum=0,i,n;
printf("enter no of elements\n");
scanf("%d",&n);
printf("Enter the elements in ascending order\n");
for(i=0; i<n; i++)
scanf("%d",&w[i]);
printf("Enter the required sum\n");
scanf("%d",&d);
for(i=0; i<n; i++)
sum +=w[i];
if(sum < d)
{
printf("no solution exits\n");
}
printf("The solution is\n");
count =0;
subset(0,0,sum);
return 0;
getch();
}
|