Guest User

Untitled

a guest
Jan 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int count=0,w[10],d=0,x[10];
  4.  
  5. void subset(int cs,int k,int r)
  6. {
  7. int i=0;
  8. x[k]=1;
  9. if((cs+w[k])>d){
  10. printf("n Subset solution = %dn",++count);
  11. for(i=0; i<=k; i++)
  12. {
  13. if(x[i]==1)
  14. printf("%dn",w[i]);
  15. }
  16. }
  17. else if(cs+w[k]+w[k+1] <=d)
  18. subset(cs+w[k],k+1,r-w[k]);
  19. if((cs+r-w[k]>=d)&&(cs+w[k+1])<=d)
  20. {
  21. x[k]=0;
  22. subset(cs,k+1,r-w[k]);
  23. }
  24. }
  25.  
  26. int main()
  27. {
  28. int sum=0,i=0,n=0;
  29. printf("enter no of elementsn");
  30. scanf("%d",&n);
  31. printf("Enter the elements in ascending ordern");
  32. for(i=0; i<n; i++)
  33. scanf("%d",&w[i]);
  34. printf("Enter the required sumn");
  35. scanf("%d",&d);
  36. for(i=0; i<n; i++)
  37. sum +=w[i];
  38. if(sum < d)
  39. {
  40. printf("no solution exitsn");
  41.  
  42. }
  43. else{
  44. printf("The solution isn");
  45. count =0;
  46. subset(0,0,sum);
  47. return 0;
  48. getch();
  49. }
  50. }
Add Comment
Please, Sign In to add comment