#include
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct mooncake {
float mount, price, unit;
};
int cmp(mooncake a, mooncake b) {
return a.unit > b.unit;
}
int main() {
//读取月饼信息,并按价格排序
int n, need;
cin >> n >> need;
vector<mooncake> a(n);
for (int i = 0; i < n; i++) cin>>a[i].mount;
for (int i = 0; i < n; i++) cin>>a[i].price;
for (int i = 0; i < n; i++) a[i].unit = a[i].price / a[i].mount;
sort(a.begin(), a.end(), cmp);
//计算价格
float result = 0.0;
for (int i = 0; i < n; i++) {
if (a[i].mount <= need) {
result = result + a[i].price;
}
else {
result = result + a[i].unit * need;
break;
}
need = need - a[i].mount;
}
printf("%.2f", result);
return 0;
}
一开始没想到用结构做,用了2个分开的数组来存月饼的价格和数量,到了排序时发现会麻烦。
排序好了后,后面的价格计算比较简单,就用总需求和月饼数量依次比较。