A1070 Mooncake (25 分)

一、参考代码

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
struct P{
    double need;//库存 
    double value;//总价 
    double s_mon;//单价 
};
bool cmp(P a, P b){
    return a.s_mon > b.s_mon;
}
int main(){
    int N;
    double D;
    double sum_mon = 0;
    cin >> N >> D;
    P product[N];
    for(int i = 0; i < N; i++){
        scanf("%lf", &product[i].need);
    }
    for(int i = 0; i < N; i++){
        scanf("%lf", &product[i].value);
        product[i].s_mon = product[i].value/product[i].need;
    }
    sort(product, product+N, cmp);
    for(int i = 0; i < N; i++){
        if(product[i].need <= D){
            sum_mon = sum_mon + product[i].value;
            D = D - product[i].need;    
        }else if(product[i].need > D){
            sum_mon = D * product[i].s_mon + sum_mon;
            break;
        }
    }
    printf("%.2lf", sum_mon);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tsruixi/p/11824911.html