1020 月饼

时间:2020-02-16 13:06:09   收藏:0   阅读:55

简单贪心算法。

把月饼按单价递减的顺序排列。

需求一定时,每次售出单价最高的月饼存量,最后获利最大。

#include"iostream"
#include<algorithm>
using namespace std;

struct Moocake {
    double store,sale,price;
} m[2000];

bool cmp(const Moocake& a,const Moocake& b) {
    return a.price > b.price;
}

int main() {
    int n;
    double command;
    cin>>n>>command;
    for(int i = 0; i < n; ++i)
        cin>>m[i].store;
    for(int i = 0; i< n; ++i) {
        cin>>m[i].sale;
        m[i].price = m[i].sale/m[i].store;
    }
    sort(m,m+n,cmp);
    double ans = 0;
    for(int i = 0; i < n; ++i) {
        if(command >= m[i].store) {
            command-=m[i].store;
            ans += m[i].sale;
        } else if(command > 0) {
            ans += m[i].price*command;
            command = 0;
        } else
            break;
    }
    printf("%.2f",ans);
    return 0;
}

 

原文:https://www.cnblogs.com/keep23456/p/12316425.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!