救救孩子
查看原帖
救救孩子
303739
winsonW楼主2023/11/26 18:42
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int number, bag; 
double money;
struct treasure
{
	double tweight, tnumber, jz;
}a[105];
bool cmp(treasure x, treasure y)
{
	if(x.jz > y.jz)
	{
		return true;
	}
	return false;
}
int main()
{
	cin >> number >> bag;
	if(bag == 0)
	{
		cout << 0;
		return 0;
	}
	for(int i = 1; i <= number; i++)
	{
		cin >> a[i].tweight >> a[i].tnumber;
		a[i].jz = a[i].tnumber / a[i].tweight;
	}
	sort(a, a + number, cmp);
	for(int i = 0; i <= number; i++)
	{
		if(a[i].tweight <= bag)
		{
			bag -= a[i].tweight;
			money += a[i].tnumber;
			if(bag == 0)
			{
				break;
			}
		}
		else
		{
			money += (bag * a[i].jz);
			break;
		}
	}
	printf("%.2lf", money);
	return 0;
}

2023/11/26 18:42
加载中...