#44911: c++ 詳解


yp11351229@yphs.tp.edu.tw (詹豫喬)

學校 : 臺北市私立延平高級中學
編號 : 276259
來源 : [203.72.178.2]
最後登入時間 :
2024-12-12 17:14:08
e788. b3.畢業典禮(Ceremony) -- 2019年12月TOI練習賽 | From: [203.72.178.2] | 發表日期 : 2024-12-24 17:23

程式碼如下:
#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';
}
 
ZeroJudge Forum