#include
#include
void main()
{
clrscr();
int n,i,j,m;
float w[10],wa[10],p[10],pro,x[10],xa[10];
printf("How many object you want:");
scanf("%d",&n);
printf("\nEnter the knspks size:");
scanf("%d",&m);
printf("\nEnter the Weight & profit:\n\n");
for(i=0;i
{
printf("Weight[%d]=",i+1);
scanf("%f",&w[i]);
wa[i]=w[i];
printf("Profit[%d]=",i+1);
scanf("%f",&p[i]);
x[i]=0.0;
printf("\n");
}
for(i=0;i
{
for(j=i+1;j
{
if((p[i]/w[i])<(p[j]/w[j]))
{
int temp=p[i];
int temp1=w[i];
p[i]=p[j];
w[i]=w[j];
p[j]=temp;
w[j]=temp1;
}
}
}
int u=m;
for(i=0;i
{
if(w[i]
{
x[i]=1.0;
pro=pro+p[i];
u=u-w[i];
}
else
{
x[i]=u/w[i];
pro=pro+x[i]*p[i];
u=u-w[i]*x[i];
}
}
for(i=0;i
for(j=0;j
if(wa[i]==w[j])
xa[i]=x[j];
printf("Total profit=%f\n",pro);
printf("\nThe amount of object taken= { ");
for(i=0;i
printf("%f ",xa[i]);
printf(" }.");
getch();
}
0 comments:
Post a Comment