A1070 Mooncake [贪心]

在这里插入图片描述
题意:给出数量算最大能卖多少钱
贪心就是一个每一步找最优解的思想

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
struct mooncake
{
	double store;
	double allcost;
	double acost;
}cake[1001];
bool cmp(mooncake a, mooncake b)
{
	return a.acost > b.acost;
}
int main()
{
	int n;double m; double cost = 0;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> cake[i].store;
	}
	for (int i = 0; i < n; i++)
	{
		cin >> cake[i].allcost;
		cake[i].acost = cake[i].allcost / cake[i].store;
	}

	sort(cake, cake + n, cmp);
	for (int i = 0; i < n; i++)
	{
		if (cake[i].store >= m)
		{
			cost += cake[i].acost * m;
			break;
		}
		else
		{
			m -= cake[i].store;
			cost += cake[i].allcost;
		}
	}
	printf("%.2f", cost);
}

posted @ 2020-07-09 10:54  _Hsiung  阅读(50)  评论(0编辑  收藏  举报