-
Notifications
You must be signed in to change notification settings - Fork 106
/
knapsack.c
67 lines (54 loc) · 1.37 KB
/
knapsack.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
62
63
64
65
66
67
#include <stdio.h>
struct data{
int profit;
int weight;
float ratio;
};
float knapsack(struct data[], int t_weight){
int i = 0;
float t_profit = 0;
while(t_weight > weight[i]){
t_profit += profit[i];
t_weight -= weight[i];
i++;
printf("%f\n", t_profit);
}
if(t_weight > 0){
t_profit += (float)t_weight / weight[i] * profit[i];
}
return t_profit;
}
int main()
{
int t_weight = 15;
int n = 7;
printf("Enter the number of Elements: ");
scanf("%d", n);
struct data d[10];
printf("Enter profit and weight of Each elements: \n");
for (int i = 0; i < n; i++)
{
scanf("%d %d", d[i].profit, d[i].weight);
d[i].ratio = (float)d[i].profit/d[i].weight;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-1; j++)
{
if(d[j].ratio < d[j+1].ratio){
int temp = d[j].ratio;
d[j].ratio = d[j+1].ratio;
d[j+1].ratio = temp;
temp = d[j].profit;
d[j].profit = d[j+1].profit;
d[j+1].profit = temp;
temp = d[j].weight;
d[j].weight = d[j+1].weight;
d[j+1].weight = temp;
}
}
}
float result = knapsack(d, t_weight);
printf(">>>%f", result);
return 0;
}