题解 | #成绩排序#

成绩排序

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

import java.util.*;

// 使用map,记录成绩和人名
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        int mode = Integer.parseInt(in.nextLine());
        HashMap<Integer, String> map = new HashMap<>();
        int[] arr = new int[n];
        int repeat = 0;//记录有多少重复数字
        for (int i = 0; i < n ; i++) {
            String s = in.nextLine();
            String[] arrS = s.split(" ");
            if (!map.containsKey(Integer.parseInt(arrS[1]))) {
                map.put(Integer.parseInt(arrS[1]), arrS[0]);
                arr[i - repeat] = Integer.parseInt(arrS[1]);
            } else {//如果成绩重复,那就把之前的人名和成绩拼起来,中间加上换行符~
                map.put(Integer.parseInt(arrS[1]),
                        map.get(Integer.parseInt(arrS[1])) + " " + arrS[1] + "\n" +  arrS[0]);
                repeat++;
            }
        }
        int[] arr2 = new  int[n - repeat];
        for (int i = 0; i < n - repeat ; i++) {
            arr2[i] = arr[i];
        }
        Arrays.sort(arr2);
        if (mode == 1) {
            for (int i = 0; i < n - repeat ; i++) {
                System.out.println(map.get(arr2[i]) + " " + arr2[i]);
            }
        } else {
            for (int i = n - repeat - 1; i >= 0 ; i--) {
                System.out.println(map.get(arr2[i]) + " " + arr2[i]);
            }
        }
    }
}

全部评论

相关推荐

04-06 11:24
已编辑
太原学院 C++
真烦好烦真烦:感觉不太对劲,这种主动加微信的一般都是坑,要小心辨别
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务