题解 | #合并表记录#

合并表记录

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

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);
        Map<Integer,Integer> imputs = new HashMap<>();
        int flag = 0;
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            if(flag == 0){
                flag = in.nextInt();
                continue;
            }
            flag -- ;
            int a = in.nextInt();
            int b = in.nextInt();
            Integer getB = imputs.get(a);
            if(getB == null){
                imputs.put(a,b);
            }else{
                int r = getB + b;
                imputs.put(a,r);
            }
           if (flag == 0){
                Set<Integer> keys = imputs.keySet();
                List<Integer> keyList = new ArrayList<>(keys);
                Collections.sort(keyList);
                for (Integer e:         keyList) {
                    System.out.println(e+" "+ imputs.get(e));
                }
            }

        }
    }
}

写的有点繁琐了,喜欢在idea上编辑好在粘贴进来。

全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
有担当的灰太狼又在摸鱼:零帧起手查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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