40WA~~help!!
查看原帖
40WA~~help!!
1091848
封禁用户楼主2024/7/10 22:03

题目传送门

上代码:

#include<bits/stdc++.h>
using namespace std;
double a, b,ans;
int n,t;
struct gold {
	int weight;
	int value;
	double temp;
} g[105];
bool cmp(gold x, gold y) {
	a = x.value * y.weight;
	b = x.weight * y.value;
	return a >= b;
}
int main() {
	cin >> n >> t;
	for (int i = 0; i < n; i++) {
		cin >> g[i].weight >> g[i].value;
		g[i].temp = 1.0 * g[i].value / g[i].weight;
	}
	sort(g, g + n, cmp);
	for (int i = 0 ; i < n ; i ++) {
		if (t >= g[i].weight) {
			ans += g[i].value;
			t -= g[i].weight;
		} else {
			ans += t * g[i].temp;
			break;
		}
	}
	cout<<ans;
	return 0;
}

大佬救一救

2024/7/10 22:03
加载中...