题解 | #合并表记录#

合并表记录

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

// import java.util.; // public class Main { // public static void main(String[] args){ // Scanner sc = new Scanner(System.in); // TreeMap<Integer,Integer> map = new TreeMap<>(); // while(sc.hasNext()){ // int n = sc.nextInt(); // for(int i =0;i<n;i++){ // int key = sc.nextInt(); // int value = sc.nextInt(); // map.put(key,map.getOrDefault(key,0)+value); // } // for(Integer i : map.keySet()){ // System.out.println(i+" "+map.get(i)); // } // } // } // } import java.util.; import java.io.*; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = null; TreeMap<Integer,Integer> map = new TreeMap<>(); while((str = in.readLine()) != null){ int num = Integer.parseInt(str); while(num -- > 0){ String[] s = in.readLine().trim().split(" "); int key = Integer.parseInt(s[0]); int value = Integer.parseInt(s[1]); if(map.containsKey(key)){ value += map.get(key); map.put(key,value); }else{ map.put(key,value); }

        }
    }
    for(Integer i : map.keySet()){
        System.out.println(i +" "+map.get(i));
    }
}

}

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

收到了小米的实习offer,犹豫是否要去。。。
认真搞学习:雷总还当过首富呢,公司不算大厂算独角兽吗
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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