题解 | #合并表记录#

合并表记录

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

import java.util.Scanner;

import java.util.Map;
import java.util.TreeMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        Map<Integer, Integer> tMap = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            if (sc.hasNextLine()) {
                String str = sc.nextLine();
                String[] strArr = str.split(" ");
                Integer i1 = Integer.parseInt(strArr[0]);
                Integer i2 = Integer.parseInt(strArr[1]);
                if (tMap.containsKey(i1)) {
                    tMap.put(i1, i2 + tMap.get(i1));
                } else {
                    tMap.put(i1, i2);
                }
                //System.out.println(tMap.toString());
            }

        }
        tMap.forEach((key, value) -> {
            System.out.println(key + " " + value);
        });
    }
}

全部评论

相关推荐

11-20 13:39
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
许愿面试顺利的小白很...:你是我在牛客上见过最美的女孩
点赞 评论 收藏
分享
11-21 14:37
江苏大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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