Week 72
Week 72
Algorithm:
Step3: Using the get function read the number of items, capacity of the
bag, Weight of the item and value of the items.
Step4: Find the small weight with high value using the find function.
Step5: Find the optimal solution using the function findop ().
float x[20], tp = 0;
int i,j, u;
u = capacity;
for(i=0;i<n;i++)
x[i] = 0.0;
for(i=0;i<n;i++)
if (weight[i] > u)
break; else {
x[i] =1.0;
tp= tp+profit[i];
u = u - weight[i];
if(i<n)
x[i] =u / weight[i];
int main()
scanf("%d", &num);
{
scanf("%f%f",&weight[i],&profit[i]);
scanf("%f", &capacity);
ratio[i]=profit[i]/weight[i];
for(i=0;i< num;i++)
for(j=i+1;j<num;j++)
knapsack(num,weight,profit,capacity);
return(0);
2 12
3 10
1 20
4 15
15
10 20
20 30
30 66
40 40
50 60
Maximumprofitis:-164.000000