题目链接
- 对月饼的单价进行排序,优先卖单价高的月饼即可。
c++代码
#include <iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include <algorithm>
using namespace std;
class moonCake {
public:
double num;
double price;
double ave_price;
};
bool cmp(moonCake& a, moonCake &b) {
return a.ave_price > b.ave_price;
}
int main() {
vector<moonCake>moon;
int n, d;
moonCake temp;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> temp.num;
moon.push_back(temp);
}
for (int i = 0; i < n; i++) {
cin >> moon[i].price;
moon[i].ave_price = moon[i].price / moon[i].num;
}
sort(moon.begin(), moon.end(), cmp);
int i = 0;
double res = 0;
for (int i = 0; i < n; i++) {
if (d >= moon[i].num) {
d -= moon[i].num;
res += moon[i].price;
}
else{
res += d * moon[i].ave_price;
break;
}
}
printf("%.2f", res);
return 0;
}
复制代码
python3代码
class moon:
inventory = 0
price = 0
singlePrice = 0.0
def main():
kind,weight= tuple(map(int,input().split()))
res = []
result = 0.0
inventory = list(map(float,input().split()))
price = list(map(float,input().split()))
for i in range(kind):
tmp = moon()
tmp.inventory = inventory[i]
tmp.price = price[i]
tmp.singlePrice = price[i] / inventory[i]
res.append(tmp)
res.sort(key=lambda x:x.singlePrice,reverse=True)
for i in range(kind):
if(weight >= res[i].inventory):
weight -= res[i].inventory
result += res[i].price
else:
result += res[i].singlePrice*weight
break
print('{:.2f}'.format(result))
main()
复制代码
转载于:https://juejin.im/post/5ce8fff3f265da1bbf68f8d7