1609: 成绩排序(PIPIPOJ)
题目描述
PIPI在帮助老师整理考试成绩,一共有n个人,现在给出他们的考试成绩和姓名,你能把他们按照考试成绩降序排列吗?
若考试成绩相同,则按照姓名升序排列。
输入
多组输入。
第一行输入人数n(1<=n<=1e5)。
接下来输入n行,每行包括一个整数代表分数,和一个字符串代表姓名。
其中分数不超过100000,姓名只包含小写英文字母。
输出
对于每组输入,按照分数降序输出每个人的分数和姓名,若分数相同,则先输出姓名字典序小的人。
样例输入
4
100 jack
125 bob
125 tom
110 xiaoming
样例输出
125 bob
125 tom
110 xiaoming
100 jack
题解1(C++版本)
#include<bits/stdc++.h>
using namespace std;
struct Person {
int score;
char name[110];
bool operator < (const Person& other) const {
if (score == other.score) {
return strcmp(name, other.name) < 0;
}
return score > other.score;
}
};
int main() {
int n;
while (~scanf("%d",&n)) {
vector<Person> p(n);
for (int i = 0; i < n; i++) {
scanf("%d%s", &p[i].score, p[i].name);
}
sort(p.begin(), p.end());
for (const auto& person : p) {
printf("%d %s\n", person.score, person.name);
}
}
return 0;
}