解法一:排序
自定义排序规则。Java最后一个点一直被卡,换C++。
#include <bits/stdc++.h>using namespace std;class Student {public:string id;string name;int score;public:Student() {}Student(const string &id, const string &name, int score) : id(id), name(name), score(score) {}static bool cmp1(Student o1, Student o2) {return o1.id < o2.id;}static bool cmp2(Student o1, Student o2) {if (o1.name == o2.name) {return o1.id < o2.id;} else {return o1.name < o2.name;}}static bool cmp3(Student o1, Student o2) {if (o1.score == o2.score) {return o1.id < o2.id;} else {return o1.score < o2.score;}}};Student students[100005];int main() {ios::sync_with_stdio(false);int N, C;cin >> N >> C;string id;string name;int score;for (int i = 0; i < N; ++i) {cin >> id >> name >> score;students[i] = Student(id, name, score);}switch (C) {case 1:sort(students, students + N, Student::cmp1);break;case 2:sort(students, students + N, Student::cmp2);break;case 3:sort(students, students + N, Student::cmp3);}for (int i = 0; i < N; ++i) {cout << students[i].id << " " << students[i].name << " " << students[i].score << '\n';}return 0;}
