题目链接:PAT 1020 月饼
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct mooncake
{
double store; //库存量
double sell; //总售价
double price; //单价
}cake[10010];
bool cmp(mooncake a, mooncake b)
{
return a.price > b.price ;
}
int main()
{
int n;
double d;
scanf("%d%lf",&n,&d);
for(int i=0;i<n;i++)
scanf("%lf",&cake[i].store);
for(int i=0;i<n;i++)
{
scanf("%lf",&cake[i].sell);
cake[i].price = cake[i].sell / cake[i].store;
}
sort(cake,cake+n,cmp); //按单价从高到低排序
double ans = 0;
for(int i=0;i<n;i++)
{
if(cake[i].store<=d) //如果需求量高于月饼库存量
{
d -= cake[i].store; //第i种月饼全部卖出
ans += cake[i].sell;
}
else //如果月饼库存量高于需求量
{
ans += cake[i].price*d; //只卖剩余需求量的月饼
break;
}
}
printf("%.2f",ans);
return 0;
}