题目链接
AC代码
#include<cstdio>
#include<algorithm>
using namespace std;
struct mooncake{
double store;//库存
double price;//单价
}cake[1010];
bool cmp(mooncake a,mooncake b){//按照单价递减排列
return a.price>b.price;
}
int main(){
int n;
double demand,total_price,profit=0;
scanf("%d%lf",&n,&demand);//输入月饼种类和市场需求
for(int i=0;i<n;i++){//输入每种月饼的库存
scanf("%lf",&cake[i].store);
}
for(int i=0;i<n;i++){//为每种月饼计算单价
scanf("%lf",&total_price);
cake[i].price=total_price/cake[i].store;
}
sort(cake,cake+n,cmp);//排序
for(int i=0;i<n;i++){
if(demand>cake[i].store){//如果目前的需求大于当前月饼库存,那么当前月饼全要了
demand-=cake[i].store;
profit+=cake[i].price*cake[i].store;
}
else{//如果当前市场需求小于当前月饼库存,只出售当前市场需求的量
profit+=cake[i].price*demand;
break;
}
}
printf("%.2lf",profit);
return 0;
}