70分求助,测试点6,7,8未过
查看原帖
70分求助,测试点6,7,8未过
841766
hereweare1楼主2023/7/6 17:09

结构体+sort 按顺序依次排序

#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
class student
{
public:
	int Chinese;
	int Math;
	int English;
	int score;
	int ID;
};
bool score_Sort(student s1, student s2)
{
	return s1.score > s2.score?1:s1.score==s2.score?s1.ID<s2.ID:0;
}
bool Chinese_Sort(student s1, student s2)
{
	return s1.Chinese > s2.Chinese;
}
int main()
{
	vector<student> Stu;
	int n;
	cin >> n;
	Stu.resize(n);
	for (int i = 0; i < n; i++)
	{
			cin >> Stu[i].Chinese >> Stu[i].Math >> Stu[i].English;
			Stu[i].score= Stu[i].Chinese +Stu[i].Math + Stu[i].English;
			Stu[i].ID = i + 1;
	}
	sort(Stu.begin(), Stu.end(), Chinese_Sort);
	sort(Stu.begin(), Stu.end(), score_Sort);
	for (int i = 0; i < 5; i++)
	{
		cout << Stu[i].ID << " " << Stu[i].score << endl;
	}
}
2023/7/6 17:09
加载中...