PAT 甲级 A1070 (2019/02/19)

Posted zjsaipplp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 甲级 A1070 (2019/02/19)相关的知识,希望对你有一定的参考价值。

#include<cstdio>
#include<algorithm>
using namespace std;
struct Mooncake{
    double inventory;   //库存
    double total_value; //总价
    double unit_price;  //单价 
}cake[1001];
bool cmp(Mooncake a, Mooncake b){
    return a.unit_price > b.unit_price;     //按照单价从高到低 
} 
int main(){
    int N, D;
    scanf("%d %d", &N, &D);
    for(int i = 0; i < N; i++){
        scanf("%lf", &cake[i].inventory);
    }
    for(int i = 0; i < N; i++){
        scanf("%lf", &cake[i].total_value);
        cake[i].unit_price = cake[i].total_value / cake[i].inventory;   //计算单价 
    }
    sort(cake, cake + N, cmp);          //排序 
    double total_cost = 0.0;
    for(int i = 0; i < N; i++){
        if(D > cake[i].inventory){      //需求大于库存 
            D -= cake[i].inventory;     //第i种月饼全部卖出 
            total_cost += cake[i].total_value;  //这些月饼的总价算进花费 
        }else{      //需求小于库存 
            total_cost += cake[i].unit_price * D;   //只卖出剩余需求量的月饼 
            break;
        }
    }
    printf("%.2f", total_cost);
    return 0;
}

以上是关于PAT 甲级 A1070 (2019/02/19)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 甲级 A1050 (2019/02/19)

PAT 甲级 A1048 (2019/02/19)

PAT 甲级 A1077 (2019/02/19)

PAT 甲级 A1041 (2019/02/19)

pat甲级75分在啥水平

pat甲级没做出来没有分吗