Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- ///int w[]={10, 7, 5, 18, 12, 20, 15};
- int w[50];
- int x[50];
- int m,n;
- int countt=0;
- class Subset
- {
- public:
- void sum_of_sub(int s,int k,int r)
- {
- x[k] = 1;
- if(s+w[k] == m)
- {
- for(int i=0;i<=k;i++)
- {
- /// cout<<x[i]<<" ";
- /// For Showing the Weight do below
- if(x[i]==1)
- cout<<w[i]<<" ";
- }
- cout<<endl;
- }
- else if (s+w[k]+w[k+1]<=m)
- sum_of_sub(s+w[k],k+1,r-w[k]);
- if((((s+r)-w[k])>=m) &&(s+w[k+1])<=m)
- {
- x[k] = 0;
- sum_of_sub(s,k+1,r-w[k]);
- }
- }
- void sum(int s,int k,int r)
- {
- x[k] = 1;
- if( s + w[k] > m)
- {
- display(k);
- }
- if(s+w[k] == m)
- {
- //display(k);
- }
- else if (s+w[k]<=m && k<n)
- sum(s+w[k],k+1,r-w[k]);
- //cout<<k<<" "<<endl;
- if( k<n )
- {
- x[k] = 0;
- sum(s,k+1,r-w[k]);
- }
- }
- void display(int k)
- {
- for(int i=1;i<=k;i++)
- {
- if( x[i] == 1)
- cout<<w[i]<<" ";
- }
- cout<<endl;
- }
- void five(int s,int k,int r)
- {
- x[k] = 1;
- if(s+w[k] == m && countt<5)
- {
- countt++;
- for(int i=0;i<=k;i++)
- {
- // cout<<x[i]<<" ";
- /// For Showing the Weight do below
- if(x[i]==1)
- cout<<w[i]<<" ";
- }
- cout<<endl;
- }
- else if (s+w[k]+w[k+1]<=m)
- five(s+w[k],k+1,r-w[k]);
- if((((s+r)-w[k])>=m) &&(s+w[k+1])<=m)
- {
- x[k] = 0;
- five(s,k+1,r-w[k]);
- }
- }
- };
- int main()
- {
- Subset obj;
- freopen("in.txt","r",stdin);
- int i=0,total_sum = 0;
- cin>>m;
- while(cin)
- {
- cin>>w[i++];
- }
- for(int j =0;j<i;j++)
- total_sum += w[j];
- obj.five(0,0,total_sum);
- //cout<<countt;
- // cout<<countt;
- return 0;
- }
Add Comment
Please, Sign In to add comment