1020. 月饼 (25)

#include <algorithm>
#include <iostream>
#include <vector>
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<node> 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值