题解 | #合并表记录#

合并表记录

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

import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
import java.util.Collections;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = Integer.parseInt(in.nextLine());
            Map<Integer,Integer> map = new TreeMap<Integer,Integer>();
            for(int i=0;i<n;i++){
                String[] strs = in.nextLine().split(" ");
                int index = Integer.parseInt(strs[0]);
                int value = Integer.parseInt(strs[1]);
                if(!map.containsKey(index)){
                    map.put(index,value);
                }else if(map.containsKey(index)){
                    map.put(index,map.get(index)+value);
                }
            }
            for(Map.Entry<Integer,Integer> entry:map.entrySet()){
                System.out.println(String.valueOf(entry.getKey())+" "+String.valueOf(entry.getValue()));
            }
        }
    }
}

全部评论

相关推荐

10-20 15:26
门头沟学院 Java
桥头牛油火锅:这个比例不正常,简历的话项目经历放中间,项目功能分点可以再明确点,前面加“·”或者“1 2 3”,另外简历上的照片可以去外面摄影店拍一下,以后也会用到的,hr筛人也是多少会看的,毕竟世界是一个巨大的卡颜局嘛,还有有些hr由于消息太多可能没看到,后面可能会回来找你,要简历的还会多一点,我也是普2本,比例大致是600:90:15:3,当然我实力不太够,拿的offer比较少,慢慢来吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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