题解 | #合并表记录#
合并表记录
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]); } } }