题解 | #成绩排序#

成绩排序

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int stuCount = in.nextInt();
            int orderDirect = in.nextInt();
            Set<Student> students;
            if (orderDirect == 0) {
                students = new TreeSet<>(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.score >= o1.score ? 1 : -1;
                    }
                });
            }else{
                students = new TreeSet<>(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.score >= o2.score ? 1 : -1;
                    }
                });
            }

            for (int i = 0; i < stuCount; i++) {
                students.add(new Student(in.next(), in.nextInt()));
            }

            for(Student s:students){
                s.println();
            }
        }
    }

    public static class Student {
        public int score;
        public String name;
        public Student(String name, int score) {
            this.score = score;
            this.name = name;
        }

        public void println() {
            System.out.println(name + " " + score);
        }
    }
}

全部评论

相关推荐

xiaowl:1. 技能堆叠没有意义,精简下,而且里面的精通、熟练等内容,其实经不起推敲,这里可以简单写清楚你在前端、后端等领域,有哪些你自己比较经验丰富熟练的技能,以及哪些有过一定涉猎,做一定区分度 2. 项目方案有些单薄,但是这个项目本身还是有很多挑战点的,你应该思考下对于里面有难题的挑战点,你是怎么解决的,避免泛泛而谈。比如,多人编辑是一个老大难问题,包括了互斥、协作等,这里可以详细讲一讲你怎么设计解决问题的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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