题解 | #成绩排序#

成绩排序

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            int number = Integer.parseInt(sc.nextLine());
            String sort = sc.nextLine();
            Student[] students = new Student[number];
            for (int i = 0; i < number; i++) {
                String line = sc.nextLine();
                String[] split = line.split(" ");
                students[i] = new Student(split[0], Integer.parseInt(split[1]));
            }

            Arrays.sort(students, (o1, o2) -> {
                        if (sort.equals("0")) {
                            return o2.score - o1.score;
                        } else {
                            return o1.score - o2.score;
                        }
                    }
            );
            for (Student student : students) {
                System.out.println(student.name + " " + student.score);
            }

        }
    }

    public static class Student {
        String name;
        int score;

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }
    }
}
全部评论

相关推荐

10-29 15:38
门头沟学院 Java
榕城小榕树:难道你简历里写了配送路径优化算法?
点赞 评论 收藏
分享
3 收藏 评论
分享
牛客网
牛客企业服务