PAT甲题题解-1070. Mooncake (25)-排序,大水题

本文介绍了一种通过算法实现购买糕点价值最大化的解决方案。该算法通过比较不同糕点的数量和单价来确定最佳购买组合,确保在有限预算下获得最高性价比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>

using namespace std;
/*
3 0
180 150 100
7.5 7.2 4.5
*/
const int maxn=1000+5;
int amounts[maxn];
float price[maxn];

struct Cake{
    float amounts;  //注意,这里得设置成浮点型,若是int一个样例会过不了
    float price;
    float unitprice=0;
    bool operator<(const Cake tmp)const{
        return unitprice>tmp.unitprice;
    }
}cake[maxn];
int main()
{
    int n,d;
    scanf("%d %d",&n,&d);
    for(int i=0;i<n;i++){
        scanf("%f",&cake[i].amounts);
    }
    for(int i=0;i<n;i++){
        scanf("%f",&cake[i].price);
        cake[i].unitprice=cake[i].price/cake[i].amounts;
    }
    sort(cake,cake+n);
    float ans=0;
    for(int i=0;i<n;i++){
        if(d>=cake[i].amounts){
            ans+=cake[i].price;
            d-=cake[i].amounts;
        }
        else{
            ans+=(d/cake[i].amounts)*cake[i].price;
            break;
        }
    }
    printf("%.2f",ans);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/chenxiwenruo/p/6390694.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值