资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
小明刚经过了一次数学考试,老师由于忙碌忘记排名了,于是老师把这个光荣的任务交给了小明,小明则找到了聪明的你,希望你能帮他解决这个问题。
输入格式
第一行包含一个正整数N,表示有个人参加了考试。接下来N行,每行有一个字符串和一个正整数,分别表示人名和对应的成绩,用一个空格分隔。
输出格式
输出一共有N行,每行一个字符串,第i行的字符串表示成绩从高到低排在第i位的人的名字,若分数一样则按人名的字典序顺序从小到大。
样例输入
3
aaa 47
bbb 90
ccc 70
样例输出
bbb
ccc
aaa 【数据规模和约定】
人数<=100,分数<=100,人名仅包含小写字母。
解题思路:
又是一个结构体题目,使用冒泡排序可以解决排序的问题,代码如下:
#include<bits/stdc++.h>
using namespace std;
struct student{
string name;
int score;
}st[100];
int main(){
int n;
cin >> n;
for(int i = 0;i < n; i++){
cin >> st[i].name;
cin >> st[i].score;
}
for(int i = 0;i < n -1; i ++){
for(int j = 0; j < n -i - 1;j ++){
student s = st[j];
if(st[j].score < st[j + 1].score){
st[j] = st[j + 1];
st[j + 1] = s;
}else if(st[j].score == st[j + 1].score){
if(st[j].name > st[j + 1].name){
st[j] = st[j + 1];
st[j + 1] = s;
}else{
continue;
}
}
}
}
for(int i =0 ; i < n; i ++){
cout << st[i].name << endl;
}
return 0;
}