题解 | #合并表记录#

合并表记录

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

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            Map<Integer, Integer> table = new HashMap<>();
            while (n-- > 0) {
                int index = in.nextInt();
                int value = in.nextInt();
                table.put(index, table.getOrDefault(index, 0) + value);
            }
            table.keySet().stream().sorted().forEach(k -> System.out.println(k + " " + table.get(k)));
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务