Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define N 6
- #define M 30
- int x[N], w[N];
- class SumOfSubset{
- public:
- void DisplayResultingSubsets(int w[], int x[],int k){
- for(int i=0;i<=k;i++)
- {
- if(x[i])
- cout<<w[i]<<" ";
- //x[i] = 0;
- }
- cout<<endl;
- }
- void SumOfSubsets(int s, int k,int r, int x[], int w[]){
- x[k]=1;
- if(s+w[k]==M){
- DisplayResultingSubsets(w,x,k);
- }
- else if(s+w[k]+w[k+1]<=M)
- SumOfSubsets(s+w[k],k+1,r-w[k],x,w);
- if((s+r-w[k]>=M)&&(s+w[k+1]<=M)){
- x[k]=0;
- SumOfSubsets(s,k+1,r-w[k], x, w);
- }
- }
- };
- int main()
- {
- SumOfSubset ob;
- int w[N] = {5,10,12,13,15,18};
- ob.SumOfSubsets(0,0,73,x,w);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement