#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct fruit {
int weight;
int value;
string name;
fruit(int w, int v, string n) : weight(w), value(v), name(n){}
};
bool comparefruits(const fruit &a, const fruit &b) {
return (double)a.value / a.weight > (double)b.value / b.weight;
}
double maxfruitValue(vector<fruit> &fruits, int capacity) {
sort(fruits.begin(), fruits.end(), comparefruits);
string n[4]={""};
int w[4]={0};
int i=0;
double maxValue = 0.0;
for (const fruit &fruit : fruits) {
if (capacity >= fruit.weight) {
maxValue += fruit.value;
capacity -= fruit.weight;
n[i]=fruit.name;
w[i]=fruit.weight;
i++;
} else {
maxValue += (double)capacity / fruit.weight * fruit.value;
n[i]=fruit.name;
w[i]=capacity;
break;
}
}
for(int i=0;i<=3;i++){
if(n[i]!=""){
cout << w[i] << "kg" << n[i] <<endl;
}
}
cout << "总价值为:" <<endl;
return maxValue;
}
int main() {
vector<fruit> fruits = {{15, 300,"苹果"}, {18, 180,"香蕉"}, {10, 150,"橘子"}, {9, 270,"猕猴桃"}};
int capacity = 20;
cout << "应装入水果: " <<endl;
cout << maxfruitValue(fruits, capacity) << "元" <<endl;
return 0;
}