-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathknapsack.cpp
82 lines (74 loc) · 1.98 KB
/
knapsack.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
float fractionalKnapsack(float weight[], float profit[], float capacity, int n)
{
int i = 0;
float maxprofit = 0.0, fraction;
while (weight[i] <= capacity)
{
maxprofit = maxprofit + profit[i];
capacity = capacity - float(weight[i]);
i++;
}
if (i < n)
{
fraction = capacity / weight[i];
maxprofit = maxprofit + fraction * profit[i];
}
return maxprofit;
}
float fractionalKnapsack1(float weight[], float profit[], float capacity, int n)
{
float fraction;
if (n == 0)
{
return 0;
}
if (weight[n] > capacity)
{
fraction = capacity / weight[n];
return fraction * profit[n];
}
if (weight[n] <= capacity)
{
capacity = capacity - weight[n];
return profit[n] + fractionalKnapsack1(weight, profit, capacity, n - 1);
}
}
int main()
{
int n;
float capacity;
std::cout << "Items: ";
std::cin >> n;
std::cout << "\nCapacity: ";
std::cin >> capacity;
float profit[n], weight[n];
float ratio[n];
for (int item = 0; item < n; item++)
{
std::cout << "weight profit: ";
std::cin >> weight[item] >> profit[item];
ratio[item] = profit[item] / weight[item];
}
// sort by ratio
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (ratio[i] > ratio[j])
{
ratio[i] = ratio[i] + ratio[j];
ratio[j] = ratio[i] - ratio[j];
ratio[i] = ratio[i] - ratio[j];
profit[i] = profit[i] + profit[j];
profit[j] = profit[i] - profit[j];
profit[i] = profit[i] - profit[j];
weight[i] = weight[i] + weight[j];
weight[j] = weight[i] - weight[j];
weight[i] = weight[i] - weight[j];
}
}
}
std::cout << "Max Profit: " << fractionalKnapsack1(weight, profit, capacity, n);
return 0;
}