首页 > 代码库 > PAT甲题题解-1070. Mooncake (25)-排序,大水题

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)-排序,大水题