PAT L2-003 月饼【贪心】

题目链接

题目意思

给你n种月饼,现在给出你每种月饼的库存量和每种月饼的总售价,现在市场最大需求量为m,问你最大的收益是多少。

解题思路

这就是一个贪心问题,要求最大的收益。并且每种月饼可以取一部分。

代码部分
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
int n,m;
struct node
{
    double weight,price;
    double value;
} a[1100];
bool cmp(node a,node b)
{
    return a.value>b.value;
}
int main()
{
    scanf("%d%d",&n,&m);
    double sum=0;
    for(int i=0; i<n; i++)
    {
        scanf("%lf",&a[i].weight);
    }
    for(int i=0; i<n; i++)
    {
        scanf("%lf",&a[i].price);
    }
    for(int i=0; i<n; i++)
    {
        a[i].value=(1.0*a[i].price)/a[i].weight;
    }
    sort(a,a+n,cmp);
    for(int i=0; i<n; i++)
    {
        if(a[i].weight<m)
        {
            sum+=a[i].price;
            m-=a[i].weight;
        }
        else
        {
            sum+=a[i].value*m;
            break;
        }
    }
    printf("%.2lf\n",sum);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值