题解 | #成绩排序#

成绩排序

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        scan.nextLine(); //防止nextInt()后面输入nextLine()出现问题,
        ArrayList<Student> list = new ArrayList<>();
        for(int i = 0; i < n; i++) {
            String[] strs = scan.nextLine().split(" ");
            String name = strs[0];
            int grade = Integer.parseInt(strs[1]);
            list.add(new Student(name,grade));
        }
        Collections.sort(list,new Comparator<Student>(){
            public int compare(Student o1,Student o2) {
                if(k == 1) {
                    return o1.grade - o2.grade;
                } else {
                    return o2.grade - o1.grade;
                }
            }
        });
        for(Student s : list) {
            System.out.println(s.name + " "+s.grade);
        }
    }       
}
class Student{
    public String name;
    public int grade;
    
    public Student(String name,int grade){
        this.name = name;
        this.grade = grade;
    }
}


全部评论

相关推荐

白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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