题解 | #合并表记录#

合并表记录

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

using System; using System.Collections.Generic; using System.Linq; public class Program { static void Main(string[] args) { var countInput = Console.ReadLine(); if(!int.TryParse(countInput, out int count)) return; var dic = new Dictionary<int, int>(); while(count > 0) { var str = Console.ReadLine(); var strArray = str.Split(" "); var key = int.Parse(strArray[0]); var value = Convert.ToInt32(strArray[1]); if(dic.ContainsKey(key)) { dic[key] += value; } else dic.Add(key, value); --count; } dic = dic.OrderBy(k => k.Key).ToDictionary(x => x.Key, x => x.Value); //dic = (from objDic in dic orderby objDic.Key ascending select objDic).ToDictionary(x=>x.Key, y=>y.Value); foreach(var item in dic) { Console.Write(item.Key); Console.WriteLine(" " + item.Value); } } }

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务