سورس کد کوله پشتی به روش 0 و 1
كد:
# include
# include
using namespace std;
void bi(int t[],int n){
for(int i=0; n!=0;i++){
t[i]=n%2;
n/=2;
}
}
int main()
{
int n,i,s,sum,b=-1;
cout<<"enter number of gold pocket :";
cin>>n;
cout<<"enter sum:";
cin>>s;
int temp[10]={0},a[10];
for( i=0;i
cin>>a[i];
for(int j=1;j
bi(temp,j);
sum=0;
for(int k=0;k
if(temp[k]!=0)
sum+=a[k];}
if (sum==s){
for(int k=0;k
if (temp[k]==1)
cout<<<" ";
b=1;
cout<
}
if (b==-1)
cout<<"there is not such set"<
return 0;
}