1020 月饼 (25 分)

题目链接:1020 月饼 (25 分)

 

这道题目逻辑方面比较简单,但是浙大的PAT往往考察细节,果不其然,因为变量类型的原因,一个测试点没过。

 

特别注意,题目中说种类数N和需求量D为正整数,其它变量则为正数,所以这里的其它变量类型应当是double型。

 

#include <bits/stdc++.h>
using namespace std;

struct Node
{
    double num;        //注意题目中库存为正数,没有说整数 
    double money;    //注意题目中总价为正数,没有说整数 
    bool operator <(const Node &b)const
    {
        return money/num<b.money/b.num;
    }
}a;
vector<Node> stock;
int main()
{
    int n,d;
    cin>>n>>d;
    double interest=0.0;
    int num;
    for(int i=0;i<n;i++)
    {
        cin>>a.num;
        stock.push_back(a); 
    } 
    for(int i=0;i<n;i++)
        cin>>stock[i].money;
    sort(stock.begin(),stock.begin()+stock.size());
    for(int i=stock.size()-1;i>=0&&d!=0;i--)
    {
        if(stock[i].num>=d)    //该种类月饼的库存大于需求 
        {
            interest+=stock[i].money*d/stock[i].num;
            d=0;
        }
        else    //该种类的库存小于需求 
        {
            interest+=stock[i].money;
            d-=stock[i].num;
        }    
    }
    printf("%.2f\n",interest); 
    return 0;
} 

 

转载于:https://www.cnblogs.com/ManOK/p/10186864.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值