Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int count=0,w[10],d=0,x[10];
- void subset(int cs,int k,int r)
- {
- int i=0;
- x[k]=1;
- if((cs+w[k])>d){
- printf("n Subset solution = %dn",++count);
- for(i=0; i<=k; i++)
- {
- if(x[i]==1)
- printf("%dn",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=0,n=0;
- printf("enter no of elementsn");
- scanf("%d",&n);
- printf("Enter the elements in ascending ordern");
- for(i=0; i<n; i++)
- scanf("%d",&w[i]);
- printf("Enter the required sumn");
- scanf("%d",&d);
- for(i=0; i<n; i++)
- sum +=w[i];
- if(sum < d)
- {
- printf("no solution exitsn");
- }
- else{
- printf("The solution isn");
- count =0;
- subset(0,0,sum);
- return 0;
- getch();
- }
- }
Add Comment
Please, Sign In to add comment