题解 | #合并表记录#

合并表记录

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

using System.Collections.Generic;
using System;
public class Program {
    public static void Main() {
        string line;
        line = Console.ReadLine ();
        int times = Convert.ToInt32(line);
        string[] strs;
        SortedDictionary<int,int> dic = new SortedDictionary<int,int>();
        for(int i = 0;i<times;i++)
        {
            line = System.Console.ReadLine();
            strs = line.Split(' ');
            int key = Convert.ToInt32(strs[0]);
            int value = Convert.ToInt32(strs[1]);
            if(dic.ContainsKey(key)){
                dic[key]+=value;
            }
            else{
                dic.Add(key, value);
            }
        }
        foreach(int index in dic.Keys){
            System.Console.WriteLine(index + " " + dic[index]);
        }
    }
}

全部评论

相关推荐

04-18 00:32
已编辑
中南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务