梅花网的一道笔试题

题目:

一条整形数据(我这里改成是整形数据的字符串型), 判断数据中出现次数最多的那个数字(如132141,这里返回1)。
如出现的次数一样,则返回数字较大的那个(如124466,返回6),如果只有个位,则直接返回(如9,就返回9)。

 

 static void Main(string[] args)
 {

    string intNumber = Console.ReadLine();   //接收输入的值

              if (intNumber.ToString().Length <= 1)
                {
                    Console.WriteLine("单数字:"+ intNumber);
                }
                else
                {
                    Console.WriteLine(GetListNumber(intNumber));
                }

              Console.ReadKey();

 }

 

        /// <summary>
        /// 返回数据
        /// </summary>
        /// <param name="number">接收的输入数据</param>
        /// <returns></returns>
        public static string GetListNumber(string number)
        {
            List<NumberClass> li = new List<NumberClass>();
            char[] arr = number.ToString().ToCharArray();  //拆分字符
            for (int i = 0; i < arr.Length; i++)
            {  
                NumberClass n = new NumberClass();
                n.num =Convert.ToInt32(arr[i].ToString());              
               li.Add(n);
            }           
            return ShowTheNumber(li);
        }      

 

         /// <summary>
        /// 返回数据
        /// </summary>
        /// <param name="li">拆分的字符串,放到list<Entity>里面</param>
        /// <returns></returns>
        public static string ShowTheNumber(List<NumberClass> li)
        {
            int countNum=0; // 相同的数字,总个数
            string numMax="";   //个数相同,返回最大的数字   

            Hashtable hsTable = new Hashtable();         

            for (int nm = 0; nm <= 9; nm++)
            {
                hsTable.Add(nm, li.Count(x => x.num == nm));
            }
           
            for (int i =0; i <= 9; i++)
            {
                if (i == 0)
                {
                   countNum = (int)hsTable[0];
                }
                else
                {
                    if (countNum <= Convert.ToInt32(hsTable[i]))
                    {
                        countNum = (int)hsTable[i];
                       
                        numMax =i.ToString();
                    }
                }
            }

            return "数字:"+ numMax+" 出现:"+countNum + "次," ;
       
        }

 

//------------------定义一个类

public class NumberClass
    {
        public int num { get; set; }
    }

//--------------------------------分割线-------------------------

看下输入和输出结果:

posted on 2012-04-20 17:49  祁东痞子  阅读(304)  评论(0编辑  收藏  举报