B.1020 月饼

题目中说的月饼的库存量和总售价是正数,没说是正整数,嘤嘤嘤,经验的主观带入实在可怕!!!明明离满分辣么近~

#include<stdio.h>
#include<algorithm>
using namespace std; 
struct node{
	double num;//该月饼的库存量 
	double totalprice;//该月饼的总售价 
	double price;//该月饼的单价 
}a[1000+10];
bool cmp(node a, node b)
{
	return a.price > b.price;
}//根据单价对结构体从大到小排序 
int main()
{
	int n;
	double d;//为了方便计算,类型设置为浮点型
	double ans = 0;
	scanf("%d%lf", &n, &d);
	for(int i = 0; i < n; i ++)
	{
		scanf("%lf", &a[i].num);
	}
	for(int i = 0; i < n; i ++)
	{
		scanf("%lf", &a[i].totalprice);
		a[i].price = a[i].totalprice / a[i].num;//计算月饼的单价
	}
	sort(a, a + n, cmp);//排序
	for(int i = 0; i < n; i ++)
	{
		if(a[i].num <= d){//当库存量小于需求量时 
			ans += a[i].totalprice;
			d -= a[i].num;//更新需求量 
		}else{
			ans += a[i].price * d;
			d = 0;//或break 
		}
	}
	printf("%.2f", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值