请问为什么这样排序会出错
查看原帖
请问为什么这样排序会出错
1050776
peterJr楼主2024/5/19 16:04

样例中最后两个数为什么顺序反了,用了 std::sort。

#include <bits/stdc++.h>

using namespace std;

struct Node
{
	int value, index;
};
int n;

int main()
{
	cin >> n;
	
	vector<Node> T(n);
	for (int i = 1, a; i <= n; ++i) 
	{
		cin >> a;
		T[i] = {a, i};
	}
	
	sort(T.begin(), T.end(), [&](const Node& a, const Node& b)
	{
		return a.value < b.value;
	});
	
	for (int i = 1; i <= n; ++i) cout << T[i].index << ' ';
	cout << endl;
		
	double ans = 0;
	for (int i = n - 1; i >= 1; --i)
	{
		int j = n - i;
		ans += T[j].value * i;
	}
	cout << fixed << setprecision(2) << ans / n << endl;
	
	return 0;
}
2024/5/19 16:04
加载中...