使用c++语言解决背包问题贪心法
时间: 2023-12-20 15:31:40 浏览: 74
以下是使用C++语言解决背包问题的贪心算法:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
struct Item {
int value;
int weight;
};
bool cmp(Item a, Item b) {
double r1 = (double)a.value / a.weight;
double r2 = (double)b.value / b.weight;
return r1 > r2;
}
double fractionalKnapsack(int W, Item arr[], int n) {
sort(arr, arr + n, cmp);
int curWeight = 0;
double finalValue = 0.0;
for (int i = 0; i < n; i++) {
if (curWeight + arr[i].weight <= W) {
curWeight += arr[i].weight;
finalValue += arr[i].value;
} else {
int remain = W - curWeight;
finalValue += arr[i].value * ((double) remain / arr[i].weight);
break;
}
}
return finalValue;
}
int main() {
int W = 50;
Item arr[] = {{60, 10}, {100, 20}, {120, 30}};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum value we can obtain = "
<< fractionalKnapsack(W, arr, n);
return 0;
}
```
阅读全文