题解 | #成绩排序#

成绩排序

http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

//这题如果用Collections的sort方法进行用比较器排序,是不稳定的,需要用Arrays的比较器排序
import java.io.;
import java.util.
;

public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int flag = sc.nextInt();
Student[] stu=new Student[n];
for (int i = 0; i < n; i++) {
stu[i]=new Student(sc.next(),sc.nextInt());
}
sort(stu,flag);
for (Student student:stu){
System.out.println(student.name+" "+student.score);
}
}
}

//使用比较器排序
public static void sort(Student[] stu,int flag){
    if (flag==1){
        Arrays.sort(stu, new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.score-o2.score;
            }
        });
    }else if (flag==0){
        Arrays.sort(stu, new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.score-o1.score;
            }
        });
    }
}

}

//创建一个学生类
class Student{
String name;
int score;
Student(String name,int score){
this.name=name;
this.score=score;
}
}

全部评论
说反了吧,Collections的sort 是稳定的;Arrays 的sort 用的是快排,是不稳定的。
2 回复 分享
发布于 2021-12-22 10:04

相关推荐

06-15 02:05
已编辑
南昌航空大学 数据分析师
Eason三木:你如果想干技术岗,那几个发公众号合唱比赛的经历就去掉,优秀团员去掉,求职没用。然后CET4这种不是奖项,是技能,放到下面的专业技能里或者单独列一个英语能力。 另外好好改改你的排版,首行缩进完全没有必要,行间距好好调调,别让字和标题背景黏在一起,你下面说能做高质量PPT你得展现出来啊,你这简历排版我用PPT做的都能比你做的好。 然后自我评价,你如果要干数据工程师,抗压能力强最起码得有吧。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:10
直接上图
牛客13578115...:改得一般,不值80
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务