第一题答案

yang_zhiyu 2023-10-03 11:27:12 2023-10-03 11:28:31

#include <iostream> 
#include <iomanip>
#include <algorithm> 
using namespace std;
const int M = 1000005;
struct three 
{
	double w;//每个宝物的重量
	double v;//每个宝物的价值
	double p;//性价比
}s[M];
bool cmp(three a, three b)
{
	return a.p > b.p;
}
int main()
{
	int n;
	double m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> s[i].w >> s[i].v;
		s[i].p = s[i].v / s[i].w;//每个宝物单位价值
	}
	sort(s, s + n, cmp);
	double sum = 0.0;
	for (int i = 0; i < n; i++)
	{
		if (m > s[i].w)
		{
			m -= s[i].w;
			sum += s[i].v;
		}
		else
		{
			sum += m * s[i].p;
			break;
		}
	}
	cout <<fixed<<setprecision(10)<< sum << endl;
	return 0;
}

共 2 条回复

chl2023

6

yang_zhiyu

有用评个论吧!!!