结构体+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;
}
}