对单价排序 然后一个个算就好啦
#include <iostream>
#include <vector>
#include <algorithm>
#define MAX 1010
using namespace std;
int n, ton;
struct Node{
float ton;
float prise;
float avg;
};
Node MoonCake[MAX];
bool cmp(Node n1, Node n2) {
return n1.avg > n2.avg;
}
int main() {
cin >> n >> ton;
for(int i = 0; i < n; i++) {
cin >> MoonCake[i].ton;
}
for (int i = 0; i < n; i++) {
cin >> MoonCake[i].prise;
MoonCake[i].avg =MoonCake[i].prise / MoonCake[i].ton;
}
sort(&MoonCake[0], &MoonCake[0] + n, cmp);
int pos = 0;
float profit = 0;
while (ton && pos < n) {
float left = ton - MoonCake[pos].ton;
if (left < 0){
profit += ton * MoonCake[pos].prise / MoonCake[pos].ton;
break;
}
else {
profit += MoonCake[pos].prise;
ton = left;
pos++;
}
}
printf("%.2f\n", profit);
return 0;
}
本文介绍了一种通过排序和逐个计算的方法来解决如何用最少的钱买到最多数量的某种商品(如月饼)的问题。该算法首先对每种月饼的单价进行排序,然后按顺序选择直到达到购买总量限制。
1093

被折叠的 条评论
为什么被折叠?



