本文共 1082 字,大约阅读时间需要 3 分钟。
#include#include #include using namespace std;//思路: 用结构体存储月饼 里面存储 月饼的数量 价格 和单价//将月饼的单价进行排序//根据月饼的需求 依次卖出价格从高到低的月饼//终止条件为 某种月饼的数量大于还需要的月饼的数量 这时候 sum += 所需数量月饼 / 该种月饼的数量 * 该种月饼的价格struct node{ double quantity; double price; double perprice;};bool cmp(struct node a, struct node b){ return a.perprice > b.perprice;}int main() { int kind; double need; cin >> kind >> need; vector mooncake(kind); for (int i = 0; i < kind; i++) { cin >> mooncake[i].quantity; } for (int i = 0; i < kind; i++) { cin >> mooncake[i].price; mooncake[i].perprice = mooncake[i].price / mooncake[i].quantity; } sort(mooncake.begin(), mooncake.end(), cmp); double Price = 0; for (int i = 0; i < kind; i++) { if (need > mooncake[i].quantity) { need -= mooncake[i].quantity; Price += mooncake[i].price; }else{ Price += mooncake[i].price * need / mooncake[i].quantity; break; } } printf("%.2lf\n", Price); return 0;}
转载地址:http://hmeel.baihongyu.com/