题解 | #合并表记录#

合并表记录

http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

题目 合并表记录

日期:2022/03/16

思路

利用TreeMap排序的特性

代码

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Map<Integer,Integer> map = new TreeMap<>();
        int times = sc.nextInt();
        for(int i=0;i<times;i++){
            int key = sc.nextInt();
            int value = sc.nextInt();
            map.put(key,map.getOrDefault(key,0)+value);
        }
        for(Integer i : map.keySet())
            System.out.println(i+" "+map.get(i));
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
05-01 13:13
ecece:这么明目张胆虚报就业率啊
点赞 评论 收藏
分享
03-21 11:31
已编辑
门头沟学院 后端
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务