-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFKnapsack.cpp
More file actions
43 lines (37 loc) · 1.13 KB
/
Copy pathFKnapsack.cpp
File metadata and controls
43 lines (37 loc) · 1.13 KB
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
#include<bits/stdc++.h>
using namespace std;
struct item {
int value, weight;
double fraction;
};
bool compare(item a, item b) {
return a.fraction > b.fraction;
}
int main() {
int n, w;
cout << "Enter the number of items: ";
cin >> n;
item items[n];
cout << "Enter the weight and value of each item: " << endl;
for (int i = 0; i < n; i++) {
cin >> items[i].weight >> items[i].value;
items[i].fraction = (double) items[i].value / items[i].weight;
}
cout << "Enter the capacity of the knapsack: ";
cin >> w;
sort(items, items + n, compare);
int currentWeight = 0;
double totalValue = 0.0;
for (int i = 0; i < n; i++) {
if (currentWeight + items[i].weight <= w) {
currentWeight += items[i].weight;
totalValue += items[i].value;
} else {
int remaining = w - currentWeight;
totalValue += items[i].fraction * remaining;
break;
}
}
cout << "The maximum value of items that can be put into the knapsack is: " << totalValue << endl;
return 0;
}