资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
小明刚经过了一次数学考试,老师由于忙碌忘记排名了,于是老师把这个光荣的任务交给了小明,小明则找到了聪明的你,希望你能帮他解决这个问题。
输入格式
第一行包含一个正整数N,表示有个人参加了考试。接下来N行,每行有一个字符串和一个正整数,分别表示人名和对应的成绩,用一个空格分隔。
输出格式
输出一共有N行,每行一个字符串,第i行的字符串表示成绩从高到低排在第i位的人的名字,若分数一样则按人名的字典序顺序从小到大。
样例输入
3
aaa 47
bbb 90
ccc 70
样例输出
bbb
ccc
aaa 【数据规模和约定】
人数<=100,分数<=100,人名仅包含小写字母。
之前我发了过一种解法,下面这一种是利用到了面向对象的思想
把学生的成绩和名字打包在一个类里面。然后在实现 Comparable接口的排序方法再对成绩排序
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n==0)System.exit(0);
Student[] obj=new Student[n];
for (int i=0;i=0; i--) {
System.out.println(obj[i].name);
}
}
public static class Student implements Comparable{
String name;
int sore;
public Student(String name, int sore) {
super();
this.name = name;
this.sore = sore;
}
public int compareTo(Student o) {
int x =this.sore-o.sore;
if(x==0) {//当成绩相等的时候
char a[]=this.name.toCharArray();
char b[]=o.name.toCharArray();
int t=Math.min(a.length, b.length);
for (int i = 0; i < t; i++) {//通过名字的字典序进行排序
x=b[i]-a[i];
if(x!=0)break;
}
//处理特殊情况比如说 q 12 qwet 12
if(a.length!=b.length&&x==0) {
if(a.length>b.length)return -1;
else return 1;
}
}
return x;
}
}
}