首页 > 其他 > 详细

【华为机试】—— 8.合并表记录

时间:2018-07-03 16:16:44      阅读:419      评论:0      收藏:0      [点我收藏+]

题目

技术分享图片

解法

import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        TreeMap<Integer, Integer> map = new TreeMap<>();
        
        int num = sc.nextInt();
        
        for(int i=0;i<num;i++){
            
            int key = sc.nextInt();
            int value = sc.nextInt();
            
            if(map.containsKey(key)){
                map.replace(key, map.get(key) + value);
            }else {
                map.put(key, value);
            }
            
        }
        
        for(Entry<Integer, Integer> entry: map.entrySet()){
            System.out.print(entry.getKey() + " ");
            System.out.println(entry.getValue());
        }
        
    }
}

 

【华为机试】—— 8.合并表记录

原文:https://www.cnblogs.com/bopo/p/9259004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!