// suma PRYTUNOW.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;
int *w,*x;
void SumOfSubsets(int,int,int,int);
int main()
{
int i,j,temp,ord,max,sum=0;
cout<<"\t\t\t\tSUMA PODZBIOROW\n";
cout<<"\n\nPodaj ilosc elementow: ";
cin>>ord;
cout<<"\n";
w = new int[ord+1];
x = new int[ord+1];
cout<<("\n\nPodaj elementy listy: \n");
for(i=1;i<=ord;i++)
{
cout<<"Element #"<<i<<":" ;
cin>>w[i];
sum+=w[i];
}
cout<<("\nEnter the sum to be made: ");
cin>>max;
sort(w, w+ord+1);
if((max<w[1])||(sum < max))
cout<<("Sorry, Subsets can't be formed.");
else
{
cout<<("\nGiven set: \n{");
for(i=1;i<=ord;i++)
cout<<w[i]<<" ";
cout<<"}";
cout<<"\n\nSubsets formed with sum as "<<max<<": ";
SumOfSubsets(0,1,sum,max);
}
getchar();getchar();
}
void SumOfSubsets(int s, int k, int r, int m)
{
int i;
x[k]=1;
if((s+w[k])==m)
{
cout<<"{";
for(i=1;i<=k;i++) cout<<(x[i]+" ");
cout<<"}\t";
}
else if((s+w[k]+w[k+1])<=m)
{
SumOfSubsets(s+w[k],k+1,r-w[k],m);
}
if(((s+r-w[k])>=m)&&((s+w[k+1])<=m))
{
x[k]=0;
SumOfSubsets(s,k+1,r-w[k],m);
}
}
///////////////////////////////////////////////