输入所有同学的语文、数学成绩和他们的学号。
以语文为第一关键字,数学为第二关键字排序,按成绩降序排序。
若语文、数学成绩均相同则学号小的同学排前面。
题目保证每个同学的学号不相同。
输出排序后所有同学的学号。
输入格式:
第一行读入一个数n(2⩽n⩽105), 表示有n个同学。
接下来n行,每行有三个整数,第i行为idi(1⩽idi⩽500000) ai,bi(1⩽ai,bi⩽10000)。
idi为学生的学号。
ai为学生的语文成绩。
bi为学生的数学成绩。
输出格式:
输出n行,每行一个数,第i行为排第i名的同学的学号
输入样例:
在这里给出一组输入。例如:
5
1 87 86
2 87 90
3 90 0
4 86 100
5 99 99
输出样例:
在这里给出相应的输出。例如:
5
3
2
1
4
代码长度限制
16 KB
时间限制
1000 ms
内存限制
256 MB
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
int id;
int chinese;
int math;
bool operator<(const Student& other) const {
if (chinese != other.chinese)
return chinese > other.chinese;
if (math != other.math)
return math > other.math;
return id < other.id;
}//因为是按照成绩降序排列,相同的成绩按照学号升序排列,所以成绩返回>,学号返回<
};
const int N = 100010;
Student students[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> students[i].id >> students[i].chinese >> students[i].math;
}
sort(students, students + n);
for (int i = 0; i < n; i++) {
cout << students[i].id;
if(i!=n-1)cout<<endl;
}
return 0;
}
不用sort函数版本已发