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

Posted 辰曦~文若

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲题题解-1070. Mooncake (25)-排序,大水题相关的知识,希望对你有一定的参考价值。

#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

 

以上是关于PAT甲题题解-1070. Mooncake (25)-排序,大水题的主要内容,如果未能解决你的问题,请参考以下文章

PAT (Advanced Level) 1070. Mooncake (25)

1070 Mooncake (25 分)难度: 简单 / 知识点: 贪心

PAT甲题题解-1075. PAT Judge (25)-排序

PAT甲题题解-1037. Magic Coupon (25)-贪心,水

PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

PAT甲题题解-1109. Group Photo (25)-(模拟拍照排队)