【1070】Mooncake (25 分)
作者:互联网
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
//key:策略-总是选择单价最高的月饼售出,可以获得最大的利润
struct mooncake{
double store; //库存量
double sell; //总售价
double price; //单价
}cake[1010];
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\n",ans);
system("pause");
return 0;
}
标签:25,int,double,price,mooncake,1070,Mooncake,include,单价 来源: https://blog.csdn.net/qq_35812205/article/details/99696709