题解 | #成绩排序#

成绩排序

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);
        int n = in.nextInt();
        int order = in.nextInt();

        PriorityQueue<String[]> queue = null;

        if (order == 1) {
            queue = new PriorityQueue<>(new Comparator<String[]>() {
                public int compare(String[] str1, String[] str2) {
                    return str1[1].equals(str2[1]) ?
                           Integer.parseInt(str1[2]) - Integer.parseInt(str2[2]) :
                           Integer.parseInt(str1[1]) - Integer.parseInt(str2[1]);
                }
            });
        } else {
            queue = new PriorityQueue<>(new Comparator<String[]>() {
                public int compare(String[] str1, String[] str2) {
                    return str1[1].equals(str2[1]) ?
                           Integer.parseInt(str1[2]) - Integer.parseInt(str2[2]) :
                           Integer.parseInt(str2[1]) - Integer.parseInt(str1[1]);
                }
            });
        }

        for (int i = 0; i < n; i++) {
            String[] strs = new String[3];
            strs[0] = in.next();
            strs[1] = in.next();
            strs[2] = String.valueOf(i);
            queue.add(strs);
        }

        for(int i = 0; i < n; i++){
            String[] strs = queue.poll();
            System.out.println(strs[0] + " " + strs[1]);
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务