日期:2014-05-20  浏览次数:20820 次

进百名散分 ~
发帖标记一下,我和总榜上有名的专家还有很大距离,我只是程序刚入门,要向他们学习 ~ 同时也希望通过努力缩小与他们的差距. 



-------------------------------------------------------------------------

虽说很久前回帖 就不敢"胡说八道"了,是经过认真思索后给出答案,但有时还是会有偏差,以下这句话用来给自己提醒吧

"除了那些无聊的UP/GZ回贴外,功利性现象越来越严重,不懂装懂的回帖也越来越多。虽然这些回帖的初始愿望大概是好的,但误导别人,即使是无意的,也总是不太好吧,难道大家都没学过孔夫子的话“知之为知之,不知为不知,是知也”么?"saucer

是谁说的我就不多说了,不是我要卖关子 是因为如果你连他都不知道的话,那真白在CSDN混了.

答题要时常告诫自己,责任才是最重要的.可能因为短短的一行错误的或有潜在BUG的回答,初学者要花费很长时间才能够改正.

这并不是我希望看到的,所以一定要避免这类情况发生.

同时也和初学者说一句话,扎实的基础才是最重要的,勿在浮沙筑高台. 

最后分享一段小程序,求字符串中出现最多的字符和出现的次数

C# code

class Program
    {

        private const string PromptString = "Please enter a string :";
        private const string ConfirmPromptString = "Your enter the string is : \"{0}\"";
        private const string TabString = "\t\t\t";
        private const string FrequencyString = "The highest frequency in enter string is character '{0}',count is {1}";

        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine(PromptString);
                Console.WriteLine();
                Console.Write(TabString);

                string enterString = Console.ReadLine();

                Console.WriteLine();
                Console.WriteLine(ConfirmPromptString, enterString);
                Console.WriteLine();

                char highestFrequencyChar;
                int count;

                MethodOne(enterString, out highestFrequencyChar, out count);

                Console.WriteLine("MethodOne :");
                Console.WriteLine();
                Console.WriteLine(FrequencyString, highestFrequencyChar, count);
                Console.WriteLine();

                MethodTwo(enterString, out highestFrequencyChar, out count);
                
                Console.WriteLine("MethodTwo :");
                Console.WriteLine();
                Console.WriteLine(FrequencyString, highestFrequencyChar, count);
                Console.WriteLine();
            }
        }

        private static int MethodOne(string enterString, out char highestFrequencyChar, out int count)
        {
            IDictionary<char, int> dic = new Dictionary<char, int>();

            highestFrequencyChar = ' ';

            int max = 0;

            for (int i = 0, len = enterString.Length; i < len; i++)
            {
                if (dic.TryGetValue(enterString[i], out count))
                {
                    if (++dic[enterString[i]] > max)
                    {
                        max = dic[enterString[i]];
                        highestFrequencyChar = enterString[i];
                    }
                }
                else
                {
                    dic[enterString[i]] = 1;
                }
            }
            count = max;
            return 1;
        }


        private static int MethodTwo(string enterString, out char highestFrequencyChar, out int count)
        {
            highestFrequencyChar = ' ';
            count = 0;

            int tempCount;

            for (int i = 0, len = enterString.Length; i < len; i++)
            {
                if ((tempCount = len - enterString.Replace(enterString[i].ToString(), "").Length) > count)
                {
                    count = tempCount;
                    highestFrequencyChar = enterString[i];
                }
            }

            return 1;
        }
    }



------解决方案--------------------
bandeng
------解决方案--------------------
jf jf jf jf jf jf
------解决方案--------------------