Complete code.Just run !
#include"stdio.h"
#include"iostream.h"
#include"conio.h"
int count,w[10],m,x[10];
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if(cs+w[k]==m)
{
cout<<"\n Subset solution "<<++count<<endl;
for(i=0; i<=k; i++)
{
if(x[i]==1)
cout<<w[i]<<endl;
}
}
else if(cs+w[k]+w[k+1] <=m)
subset(cs+w[k],k+1,r-w[k]);
if((cs+r-w[k]>=m)&&(cs+w[k+1])<=m)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
int main()
{ clrscr();
int sum=0,i,n;
cout<<"enter no of elements=\n";
cin>>n;
cout<<"Enter the elements in ascending order\n";
for(i=0; i<n; i++)
scanf("%d",&w[i]);
cout<<"Enter the required sum\n";
cin>>m;
for(i=0; i<n; i++)
{
sum +=w[i];
}
if(sum < m)
{
cout<<"no solution exits\n";
}
cout<<"The solution is\n";
count =0;
subset(0,0,sum);
getch();
return 0;
}
#include"stdio.h"
#include"iostream.h"
#include"conio.h"
int count,w[10],m,x[10];
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if(cs+w[k]==m)
{
cout<<"\n Subset solution "<<++count<<endl;
for(i=0; i<=k; i++)
{
if(x[i]==1)
cout<<w[i]<<endl;
}
}
else if(cs+w[k]+w[k+1] <=m)
subset(cs+w[k],k+1,r-w[k]);
if((cs+r-w[k]>=m)&&(cs+w[k+1])<=m)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
int main()
{ clrscr();
int sum=0,i,n;
cout<<"enter no of elements=\n";
cin>>n;
cout<<"Enter the elements in ascending order\n";
for(i=0; i<n; i++)
scanf("%d",&w[i]);
cout<<"Enter the required sum\n";
cin>>m;
for(i=0; i<n; i++)
{
sum +=w[i];
}
if(sum < m)
{
cout<<"no solution exits\n";
}
cout<<"The solution is\n";
count =0;
subset(0,0,sum);
getch();
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন