Advertisement
Farid_Mia59

found or not.cpp

Nov 16th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define N 6
  4. #define M 30
  5. int x[N], w[N];
  6.  
  7. class SumOfSubset{
  8. public:
  9.  
  10. void DisplayResultingSubsets(int w[], int x[],int k){
  11. for(int i=0;i<=k;i++)
  12. {
  13. if(x[i])
  14.  
  15. cout<<w[i]<<" ";
  16. //x[i] = 0;
  17. }
  18. cout<<endl;
  19. }
  20. void SumOfSubsets(int s, int k,int r, int x[], int w[]){
  21.  
  22. x[k]=1;
  23. if(s+w[k]==M){
  24. DisplayResultingSubsets(w,x,k);
  25. }
  26. else if(s+w[k]+w[k+1]<=M)
  27. SumOfSubsets(s+w[k],k+1,r-w[k],x,w);
  28.  
  29. if((s+r-w[k]>=M)&&(s+w[k+1]<=M)){
  30. x[k]=0;
  31. SumOfSubsets(s,k+1,r-w[k], x, w);
  32. }
  33. }
  34. };
  35.  
  36.  
  37. int main()
  38. {
  39. SumOfSubset ob;
  40. int w[N] = {5,10,12,13,15,18};
  41. ob.SumOfSubsets(0,0,73,x,w);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement