【ACM】PAT. B1020 & A1070 MoonCake 【简单贪心】

题目链接
解题思路

简单贪心:单价排序后,按序选择即可


AC程序(C++)
/**************************
//@Author: 3stone 
//@ACM: PAT-B1020.Mooncake
//@Time: 18/3/22
//@IDE: VS2017
//@Key: It`s no use crying over spilt milk!
***************************/
#include<cstdio>
#include<algorithm>
#define MAXSIZE 1010

using namespace std;

struct mooncake{
    double store;
    double sell;
    double price;
}cake[1010];

bool cmp(mooncake t1, mooncake t2) {
    return  t1.price > t2.price;
}

int main() {
    int type;
    double totalNum,totalVal = 0;
    scanf("%d%lf", &type, &totalNum);
    for (int i = 0; i < type; i++)
        scanf("%lf", &cake[i].store);
    for (int i = 0; i < type; i++) {
        scanf("%lf", &cake[i].sell);
        cake[i].price = cake[i].sell / cake[i].store; //记录单价
    }
    sort(cake, cake + type, cmp); //只是排序了值,不知道具体序号

    for (int i = 0; i < type; i++) {
        if (cake[i].store >= totalNum) { //当前种类满足要求
            totalVal += cake[i].price * totalNum;
            break;
        }
        totalVal += cake[i].sell;
        totalNum -= cake[i].store; //需求量减少
    }
    printf("%.2f\n", totalVal);

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值