题解 | #成绩排序#

成绩排序

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

import java.util.Scanner;
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 count = in.nextInt();
            int flag = in.nextInt();
            in.nextLine();
            List<String> list = new ArrayList<>();
            Map<Integer, List<String>> map = new LinkedHashMap<>();
            for (int i = 0; i < count; i++) {
                String line = in.nextLine();
                if (line.isEmpty()) {
                    continue;
                }
                String[] strs = line.split(" ");
                if (strs.length != 2) {
                    continue;
                }
                if (map.containsKey(Integer.parseInt(strs[1]))) {
                    List<String> value = map.get(Integer.parseInt(strs[1]));
                    value.add(strs[0]);
                    map.put(Integer.parseInt(strs[1]), value);
                } else {
                    List<String> value = new ArrayList<>();
                    value.add(strs[0]);
                    map.put(Integer.parseInt(strs[1]), value);
                }
            }
            Map<Integer, List<String>> result = new LinkedHashMap<>();
            if (flag == 0) {
                // 降序
                result = sortByDesc(map);
            }
            if (flag == 1) {
                // 升序
                result = sortByAsc(map);
            }
            for (Map.Entry<Integer, List<String>> entry : result.entrySet()) {
                List<String> names = entry.getValue();
                names.forEach(name -> System.out.println(name + " " + entry.getKey()));
            }

        }
    }
    private static Map<Integer, List<String>> sortByDesc(Map<Integer, List<String>>
            map) {
        Map<Integer, List<String>> result = new LinkedHashMap<>();
        map.entrySet().stream().sorted(Collections.reverseOrder(
                                           Map.Entry.comparingByKey())).forEachOrdered(x -> result.put(x.getKey(),
                                                   x.getValue()));
        return result;
    }

    private static Map<Integer, List<String>> sortByAsc(Map<Integer, List<String>>
            map) {
        Map<Integer, List<String>> result = new LinkedHashMap<>();
        map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(
            x -> result.put(x.getKey(), x.getValue()));
        return result;
    }
}

全部评论

相关推荐

最近拿到了正浩的提前批offer感觉自己的实力得到了肯定,也给了我更多底气
搞机墨镜猫:正浩提前批官网好像就只有电力电子软硬件,哥们投的是这两个岗位吗
26届校招投递进展
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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