0% found this document useful (0 votes)
25 views2 pages

Frac Tionalknapsack

Uploaded by

shirisha reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Frac Tionalknapsack

Uploaded by

shirisha reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/******************************************************************************

Welcome to GDB Online.


GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP,
Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS,
SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
int n,i,j,m;
struct k
{
int p;
int w;
float x;
};
void sort(struct k a[])
{
struct k temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j].x<a[j+1].x)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
printf("Enter the quantity\t");
scanf("%d",&n);
struct k a[n];
float sol[n];
for(i=0;i<n;i++)
{
printf("enter profit of item %d\t",i+1);
scanf("%d",&a[i].p);
printf("enter weight of item %d\t",i+1);
scanf("%d",&a[i].w);
a[i].x=(float)(a[i].p/a[i].w);

}
sort(a);
printf("enter the knapsack capacity\t");
scanf("%d",&m);
float u=m;
int x=0;
for(i=0;i<n;i++)
sol[i]=0;
for(i=0;i<n;i++)
{
if(a[i].w>u)
{
break;
}
else
{
u-=a[i].w;
sol[i]=1;
}
}
if(u!=0)
{
sol[i]=u/(float)a[i].w;
}
float profit=0;
for(i=0;i<n;i++)
{
profit+=sol[i]*a[i].p;
printf("%f\t",sol[i]);
}
printf("\n%f is total profit",profit);
return 0;
}

You might also like