1020 月饼 (25 分)

题目链接

  1. 对月饼的单价进行排序,优先卖单价高的月饼即可。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值