题解 | #合并表记录#

合并表记录

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

import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        TreeMap<Integer,Integer> map = new TreeMap<Integer,Integer>();
        for(int i = 0;i < num;i++){
            int key = sc.nextInt();
            int value = sc.nextInt();
            if(map.containsKey(key)){
                map.put(key,map.get(key)+value);
            }else{
                map.put(key,value);
            }
        }
        for(int key : map.keySet()){
            System.out.println(key+" "+map.get(key));
        }
        
    }
}

键值对形式数据,采用map集合结合循环处理

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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