题解 | #成绩排序# 用例居然有重名的,至少应该说一声吧

成绩排序

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 sort = in.nextInt();
        in.nextLine();
        Map<String, Integer> map = new LinkedHashMap<>();
        while (in.hasNextLine()) {
            String nameAndScore = in.nextLine();
            String[] arr = nameAndScore.split(" ");
            int score = Integer.valueOf(arr[1]);
            map.put(nameAndScore, score);//用例居然有重名的,所以用"name score"来做key来防止重名.如果说重名又重分,那就会舍弃一个,好在用例没有这样的
        }
        List<String> keys = new ArrayList<>(map.keySet());
        int flag = sort == 1 ? 1 : -1;//升序还是降序
        keys.sort((t1, t2)->(flag * (map.get(t1) - map.get(t2))));
        for (String key : keys) {
            System.out.println(key);
        }
    }
}


全部评论

相关推荐

认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
nus2201602...:兄弟,你这个简历撕了丢了吧,就是一坨,去找几个项目,理解项目流程,看几遍就是你的了,看看八股就去干了,多看看牛客里别人发出来的简历,对着写,你这写的啥啊,纯一坨
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:13
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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