题解 | #成绩排序#

成绩排序

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Person{
    int score;
    String name;
    Person(String n,int s){
        this.name = n;
        this.score = s;
    }
}
public class Main {


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            int order = in.nextInt();
            String t = in.nextLine();
            List<Person> list = new ArrayList<>();
            for(int i=0;i<num;i++){
                String name = in.nextLine();
                String[] nameCo = name.split(" ");
                list.add(new Person(nameCo[0],Integer.parseInt(nameCo[1])));
            }
            if(order==1){
                list.sort(new Comparator<Person>(){
                    public int compare(Person a,Person b){
                        return a.score - b.score;
                    }
                });
            }
            if(order==0){
                list.sort(new Comparator<Person>(){
                    public int compare(Person a,Person b){
                        return b.score - a.score;
                    }
                });
            }
            for(Person p: list){
                System.out.println(p.name +" "+p.score);
            }
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
02-14 11:10
点赞 评论 收藏
分享
02-08 20:56
已编辑
南京工业大学 Java
在等offer的比尔很洒脱:我也是在实习,项目先不说,感觉有点点小熟悉,但是我有点疑问,这第一个实习,公司真的让实习生去部署搭建和引入mq之类的吗,是不是有点过于信任了,我实习过的两个公司都是人家正式早搭好了,根本摸不到部署搭建的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务