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;
}

猜你喜欢

转载自www.cnblogs.com/zjsaipplp/p/10425236.html