题解 | 合并表记录
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
public class Program { public static void Main() { string line = System.Console.ReadLine (); var count = int.Parse(line); var lit= new System.Collections.Generic.List<int>(); var dic = new System.Collections.Generic.Dictionary<int, int>(); for (var ix = 0; ix < count; ix++) { line = System.Console.ReadLine(); var splits = line.Split(' '); var index = int.Parse(splits[0]); var val = int.Parse(splits[1]); if (dic.TryGetValue(index, out var valNow)) { dic[index] = valNow + val; }else{ dic[index] = val; lit.Add(index); } } lit.Sort((v1, v2)=>{ return v1 - v2; }); foreach(var index in lit) System.Console.WriteLine(index + " " + dic[index]); } }