水题--PAT.B1020

思路

简单贪心:单价排序后,按序选择即可

/**************************
//@Author: 3stone 
//@ACM: PAT-B1020.Mooncake
//@Time: 18/3/22
//@IDE: VS2017
//@Key: It`s no use crying over spilt milk!
***************************/
#include<cstdio>
#include<algorithm>
#define MAXSIZE 1010

using namespace std;

struct mooncake{
    double store;
    double sell;
    double price;
}cake[1010];

bool cmp(mooncake t1, mooncake t2) {
    return  t1.price > t2.price;
}

int main() {
    int type;
    double totalNum,totalVal = 0;
    scanf("%d%lf", &type, &totalNum);
    for (int i = 0; i < type; i++)
        scanf("%lf", &cake[i].store);
    for (int i = 0; i < type; i++) {
        scanf("%lf", &cake[i].sell);
        cake[i].price = cake[i].sell / cake[i].store; //记录单价
    }
    sort(cake, cake + type, cmp); //只是排序了值,不知道具体序号

    for (int i = 0; i < type; i++) {
        if (cake[i].store >= totalNum) { //当前种类满足要求
            totalVal += cake[i].price * totalNum;
            break;
        }
        totalVal += cake[i].sell;
        totalNum -= cake[i].store; //需求量减少
    }
    printf("%.2f\n", totalVal);

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26398495/article/details/79650803