PAT_甲级_1070 Mooncake (25point(s)) (C++)【签到题/结构体排序】

目录

1,题目描述

题目大意

2,思路

3,AC代码

4,解题过程


1,题目描述

Sample Input:

3 200
180 150 100
7.5 7.2 4.5

 

Sample Output:

9.45

题目大意

现在囤积的有许多种类的月饼,每类月饼都有一定的售价,市场有一定量的需求,问如何能获得最大利益?(肯定是先卖贵的鸭!)

2,思路

将每种月饼按照获利能力的高低,从大到小排序,挑贵的卖。

 

3,AC代码

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

struct node{
    float amount, price, pre;
};

bool cmp1(node a, node b){
    return a.pre > b.pre;//性价比高
}
int main(){
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE

    int N, D;
    cin>>N>>D;
    vector<node> data(N);
    float amount, price;
    for(int i = 0; i < N; i++){
        scanf("%f", &amount);
        data[i].amount = amount;
    }
    for(int i = 0; i < N; i++){
        scanf("%f", &price);
        data[i].price = price;
        data[i].pre = data[i].price / data[i].amount;
    }
    sort(data.begin(), data.end(), cmp1);

    float ans = 0;
    int index = 0;
    while(D > 0 && index < data.size()){
        if(data[index].amount < D){
            D -= data[index].amount;
            ans += data[index].price;
        }else{
            ans += D * data[index].pre;
            D = 0;
        }
        index++;
    }
    printf("%.2f", ans);
    return 0;
}

 

4,解题过程

一发入魂。

数据量也不大,题意也比较明显,直接写就完事了。 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值