前言
定义结构体,再写一个比较函数就可以使用sort排序了
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string id;
string name;
int score;
};
int N, C;
bool cmp(Student a, Student b) {
if (C == 1)
return a.id < b.id;
else if (C == 2) {
if (a.name != b.name)
return a.name < b.name;
return a.id < b.id;
}
else if (C == 3) {
if (a.score != b.score)
return a.score < b.score;
return a.id < b.id;
}
}
int main() {
int i_case = 0;
while (scanf("%d %d", &N, &C) && N && C) {
i_case++;
vector<Student> students;
for (int i = 0; i < N; i++) {
Student tmp;
cin >> tmp.id >> tmp.name >> tmp.score;
students.push_back(tmp);
}
sort(students.begin(), students.end(), cmp);
printf("Case %d:\n", i_case);
for (int i = 0; i < N; i++)
cout << students[i].id << " " << students[i].name << " " << students[i].score << endl;
}
}