日期:2014-05-20 浏览次数:20820 次
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; } }