版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址
http://blog.csdn.net/lzuacm

C#版 - PAT乙级(Basic Level)真题 之 1021.个位数统计_牛客网

在线提交:
https://www.nowcoder.com/pat/6/problem/4047

PTA(拼题A,原PAT)
https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296

时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)

题目描述

给定一个k位整数N = <nobr aria&#45;hidden="true"> dk110k1+...+d1101+d0 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msub> <mi> d </mi> <mrow class="MJX&#45;TeXAtom&#45;ORD"> <mi> k </mi> <mo> − </mo> <mn> 1 </mn> </mrow> </msub> <mo> ⋅ </mo> <msup> <mn> 10 </mn> <mrow class="MJX&#45;TeXAtom&#45;ORD"> <mi> k </mi> <mo> − </mo> <mn> 1 </mn> </mrow> </msup> <mo> + </mo> <mo> . </mo> <mo> . </mo> <mo> . </mo> <mo> + </mo> <msub> <mi> d </mi> <mn> 1 </mn> </msub> <mo> ⋅ </mo> <msup> <mn> 10 </mn> <mn> 1 </mn> </msup> <mo> + </mo> <msub> <mi> d </mi> <mn> 0 </mn> </msub> </math> (0<= <nobr aria&#45;hidden="true"> di </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msub> <mi> d </mi> <mi> i </mi> </msub> </math><=9, i=0,…,k-1, <nobr aria&#45;hidden="true"> dk1 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msub> <mi> d </mi> <mrow class="MJX&#45;TeXAtom&#45;ORD"> <mi> k </mi> <mo> − </mo> <mn> 1 </mn> </mrow> </msub> </math>>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。

输入描述:

每个输入包含1个测试用例,即一个不超过1000位的正整数N

输出描述:

N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。

输入例子:

100311

输出例子:

0:2

1:3

3:1

思路:

直接将输入的数字作为字符串来处理,排序后进行输出~

已AC代码:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Pat2_digitNumFre {
    class Program {
        static void Main(string[] args)
        {
            string sb;
            Dictionary<char, int> dict = new Dictionary<char, int>();
            while ((sb = Console.ReadLine()) != null)
            {
                string[] s = sb.Split();
                var num = s[0];

                for (int i = 0; i < num.Length; i++)
                {
                    if (!dict.ContainsKey(num[i]))
                        dict.Add(num[i], 1);
                    else
                        dict[num[i]]++;
                }
                var ordereds = dict.OrderBy(x => x.Key);
                foreach (var ord in ordereds)
                {
                    Console.WriteLine(ord.Key + ":" + ord.Value);
                }
            }
        }
    }
}

Rank: