程式碼如下:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct student {
int order;
string ID, name;
char collegeCode;
bool operator<(const student &other) const {
if (this->collegeCode != other.collegeCode)
return this->collegeCode < other.collegeCode;
if (this->ID[0] != other.ID[0])
return this->ID[0] < other.ID[0];
return this->order < other.order;
}
} students[100];
int main() {
cin.sync_with_stdio(false), cin.tie(nullptr);
int amount;
cin >> amount;
for (int i = 0; i < amount; ++i) {
cin >> students[i].ID, getline(cin, students[i].name);
students[i].order = i, students[i].collegeCode = students[i].ID[students[i].ID.size() - 1];
}
sort(students, students + amount);
for (int i = 0; i < amount; ++i)
cout << students[i].collegeCode << ':' << students[i].name << '\n';
}