Tuesday, January 19, 2010

KNAPSACK

#include
#include

void Gknapsack(int [],int,int);

void main()
{
clrscr();
int i,w[30],p[30],m,n;
/* w array for weights.
p array for profits.
m stands for maximum capacity.
n stands for number od items.
i is counter.*/
cout<<"enter the number of items::";
cin>>n;
cout< cin>>m;
cout< for(i=1;i<=n;i++)
cin>>w[i];
cout< for(i=1;i<=n;i++)
cin>>p[i];
Gknapsack(w,m,n);
getch();
}

void Gknapsack(int w[],int m,int n)
{
float x[30];
int U=m;
for(int i=1;i<=n;i++)
x[i]=0;
for(i=1;i<=n;i++)
{
if(w[i]>U)
break;
x[i]=1;
U=U-w[i];
}
if(i<=n)
x[i]=(float)U/w[i];
cout< for(i=1;i<=n;i++)
cout<}

No comments: