- 题目链接:L2-003 月饼
- 考查知识:贪心
- 题意描述:给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
- 思路简析:贪心的思想,排序后选取局部最优值即可
- 具体代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e3+10; struct mooncake{ double store,sell,price;//库存量、总售价、单价 }c[N]; bool cmp(mooncake a,mooncake b){//按单价降序 return a.price>b.price; } int main(){ int n;//月饼的种类数 double d;//市场最大需求量 cin>>n>>d; for(int i=0;i<n;i++)cin>>c[i].store; for(int i=0;i<n;i++)cin>>c[i].sell,c[i].price=c[i].sell/c[i].store; sort(c,c+n,cmp); double ans; for(int i=0;i<n;i++){//遍历所有种类月饼 if(c[i].store<=d){//该类月饼库存<=需求,则该月饼全部销售 d-=c[i].store; ans+=c[i].sell; }else{//该类月饼库存>需求,则该该类月饼取需求量*单价 ans+=c[i].price*d; break; } } cout<<fixed<<setprecision(2)<<ans; return 0; }
03-27
1161
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
05-01
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交