-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathKnapsack problem.c
61 lines (58 loc) · 1.23 KB
/
Knapsack problem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
typedef struct{
int weight,value;
float ratio;
}Items;
void knapsack(Items item[], int n)
{
int i=n-1,m;
printf("Enter the capacity of knapsack: ");
scanf("%d",&m);
float max_profit = 0.0;
while(m!=0)
{
if(m >= item[i].weight){
max_profit += (float)item[i].value;
m -= item[i].weight;
}
else{
max_profit += ((float)m / (float)item[i].weight) * (float)item[i].value;
m = 0;
}
i--;
}
printf("Maximum profit: %.2f", max_profit);
}
void sort(Items item[], int n)
{
int i,j;
Items temp;
for(i=0;i<n;i++)
item[i].ratio = (float)item[i].value/(float)item[i].weight;
for(i = 1 ; i < n; i++){
j = i;
while ( j > 0 && item[j-1].ratio > item[j].ratio){
temp = item[j];
item[j] = item[j-1];
item[j-1] = temp;
j--;
}
}
knapsack(item,n);
}
void main()
{
int i,n;
Items item[100];
printf("Enter the no. of items: ");
scanf("%d",&n);
printf("Enter the weight and value of %d items:\n",n);
for(i = 0; i < n; i++)
{
printf("Weight[%d]: ", i);
scanf("%d", &item[i].weight);
printf("Value[%d]: ", i);
scanf("%d", &item[i].value);
}
sort(item,n);
}