题解 | #合并表记录#

合并表记录

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;
import java.lang.System;
 
public class Main{
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int num = Integer.parseInt(br.readLine());
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<num;i++){
            String str = br.readLine();
            String[] arr = str.split(" ");
            map.put(Integer.parseInt(arr[0]),map.getOrDefault(Integer.parseInt(arr[0]),0)+Integer.parseInt(arr[1]));
        }
        Object[] objs = map.keySet().toArray();
        Arrays.sort(objs);
        for(Object obj:objs){
            System.out.println(obj+" "+map.get((int)obj));
        }
    }
}

#华为OD机考#
全部评论

相关推荐

09-19 12:15
门头沟学院 Java
猫头夜鹰:请问收到意向要点接受拒绝吗,还是开奖之后再接受拒绝
点赞 评论 收藏
分享
09-21 21:14
门头沟学院
否极泰来来来来:和他说:这里不好骂你,我们加个微信聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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