不准调用系统方法的面试题。。。
1、给定一个字符串string str="中文字符**********************"
用程序求出现频率最高的字符,(要求写成函数,开发语言不限,不能直接调用系统方法。)
这个题如果用C#写:(一下是我问一个同学,他写的)
如果用C语言呢??我考虑的东西就很不一样了。
还有题目所说的不能直接调用系统方法,以下的C#代码中,属于直接调用系统方法吗?
class Program
{
public static string GetMostFrequency(string str)
{
List<char> strChar = new List<char>(str.Length);
List<int> strCharCount = new List<int>(str.Length);
for (int i = 0; i < str.Length;i++ )
{
char tempChar = str[i];
if (!strChar.Contains(tempChar))
{
strChar.Add(tempChar);
strCharCount.Add(0);
}
strCharCount[strChar.IndexOf(tempChar)] += 1;
}
Dictionary<char, int> dict = new Dictionary<char, int>();
for (int i = 0; i < strChar.Count; i++)
{
dict.Add(strChar[i],strCharCount[i]);
}
int maxCount = dict.Max(p=>p.Value);
var keyValuePair = dict.Select(p => p).Where(p => p.Value == maxCount);//select * from table where 最高频率=(select max(频率) from table)
string result = "";
foreach (KeyValuePair<char, int> item in keyValuePair)
{
result += " '"+item.Key + "' , 次数 " + item.Value + "\n";
}
return result;
}
static void Main(string[] args)
{
string str = " 我 我我我我我我 去去去去去去去去你你你你你你 男男女女 ";
Console.WriteLine("最高频率的字符串是: \n"+GetMostFrequency(str));
Console.ReadKey();
}
}