题解 | #合并表记录#

合并表记录

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> map = new HashMap<>();
        int num = Integer.valueOf(in.nextLine());
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            String[] b = a.split(" ");
            int keyNum = Integer.valueOf(b[0]);
            int value = Integer.valueOf(b[1]);
            if(map.get(keyNum) == null){
                map.put(keyNum,value);
            }else{
                map.put(keyNum,map.get(keyNum)+value);
            }
        }
        List<Integer> lista = new ArrayList<>();
        for(int k : map.keySet()){
            lista.add(k);
        }
        Collections.sort(lista);
        for(int k : lista){
            int value = map.get(k);
            System.out.println(k+" "+value);
        }
    }
}

#练习时长两年半#
全部评论

相关推荐

完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
Rena1ssanc...:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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