L2-003 月餅 (25分)測試點2

跑的真快的並快樂的疾風索發表於2020-11-27

題目說庫存量和售價是正數也就說可能是小數,真奇怪,難道賣半塊月餅??

#include <bits/stdc++.h>
using namespace std;
typedef struct
{
    double cnt;
    double sum;
    double ave;
} Node;
const int N = 1010;
Node node[N];
bool cmp(Node a, Node b){
    return a.ave > b.ave;
}
int main(void)
{
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
    {
        scanf("%lf", &node[i].cnt);
    }
    for (int i = 0; i < n; i++)
    {
        scanf("%lf", &node[i].sum);
    }
    for (int i = 0; i < n; i++)
    {
        node[i].ave = 1.0 * node[i].sum / node[i].cnt;
    }
    double profit = 0;
    sort(node,node+n,cmp);
    for (int i = 0; i < n; i++)
    {
        if (m == 0)
            break;
        if (m >= node[i].cnt)
        {
            m -= node[i].cnt;
            profit += node[i].sum;
        }
        else if (m < node[i].cnt)
        {
            profit += m * node[i].ave;
            m = 0;
        }
    }

    printf("%.2f\n",profit);
    return 0;
}

相關文章