题解 | #HJ068 成绩排序#

成绩排序

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

import java.util.*;

/**
 * HJ68 成绩排序
 */
public class HJ068 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int count = Integer.parseInt(sc.nextLine());//学生个数
            int flag = Integer.parseInt(sc.nextLine());//升序or降序
            Student[] students = new Student[count];
            for (int i = 0; i < count; i++) {
                String[] split = sc.nextLine().split(" ");
                String name = split[0];
                int score = Integer.parseInt(split[1]);
                students[i] = new Student(name, score);
            }
            Arrays.sort(students, (o1, o2) -> {
                if (flag == 1) {
                    return o1.score - o2.score;
                } else {
                    return o2.score - o1.score;
                }
            });
            for (Student student : students) {
                System.out.println(student.name + " " + student.score);
            }
        }
        sc.close();
    }

    public static class Student {
        String name;
        int score;

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

相关推荐

Z_eus:别打招呼直接发你的优势
点赞 评论 收藏
分享
qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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