设计一个函数,统计给定的整数中各个数字出现的次数,并在主函数中调

2024-11-28 14:25:21
推荐回答(1个)
回答1:

                Dictionary dic = new Dictionary();
                String s = "12342212213244";
                char[] chardata = s.ToCharArray();
                foreach (char c in chardata)
                {
                    if (!dic.ContainsKey(c))
                    {
                        dic.Add(c, 1);
                    }
                    else
                    {
                        dic[c] = dic[c] + 1;
                    }
                }
                foreach (KeyValuePair item in dic)
                {
                    Console.WriteLine("{0}出现{1}次", item.Key, item.Value);
                }
                Console.ReadLine();