面试题,大家来练练手

贾利伟 2006-06-23 11:01:38
今天,在论坛上看到了一个面试题的贴子。
给一个字符串,如(“adasdjwoejjfadgh“)找出重复最多的字母和重复的次数。不知道大家能用多长时间作出来,请大家试一下。
我用了1.5 小时,惭愧!!
public struct TestData
{
public string differStr;
public char cha;
public int num;
}
//"adjfqoieuqjfadiwoiurqjfay";
public TestData ResultStr(string str)
{
string resultStr = str.ToCharArray()[0].ToString();
char[] difChar;
char[] cha = str.ToCharArray();
bool bl;
for (int i = 0; i < cha.Length; i++)
{
bl = false;
difChar = resultStr.ToCharArray();
for (int j = 0; j < difChar.Length; j++)
{
if (cha[i] == difChar[j])
{
bl = true;
}
}
if (!bl)
{
resultStr += cha[i].ToString();
}
}
int max =0;
char finChar = new char();
int temp;
difChar = resultStr.ToCharArray();
for(int x =0;x< difChar.Length ;x++)
{
temp =0;
for(int y =0;y< cha.Length;y++)
{
if(difChar[x] == cha[y])
{
temp ++;
}
}
if(temp > max)
{
finChar = difChar[x];
max = temp;
}
}

TestData ts = new TestData();
ts.differStr = resultStr;
ts.cha = finChar;
ts.num = max;
return ts;

}
...全文
321 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
NickyName 2006-07-13
  • 打赏
  • 举报
回复
既然是C#的,就应该允许用ArrayList, Hashtable. 而如果允许使用的话,就很简单了。

string source = "asdfaaasf";
ArrayList arr = new ArrayList();
Hashtable ht = new Hashtable();
foreach (char c in source)
{
if (arr.Contains(c))
{
int temp = (int) ht[c];
ht.Remove(c);
ht.Add(c, temp + 1);
}
else
{
arr.Add(c);
ht.Add(c, 1);
}
}
int cache = 0;
foreach (char c in ht.Keys)
{
int i = (int)ht[c];
if (i > cache)
{
cache = i;
}
}
foreach (char c in ht.Keys)
{
int i = (int)ht[c];
if (i == cache)
{
System.Diagnostics.Trace.WriteLine(c + "---" + i);
}
}
scow 2006-07-05
  • 打赏
  • 举报
回复
重复最多的字母可能不止一个,
public static int GetFrequenceAlpha(string str, out List<string> alpha)
{
alpha = new List<string>();

SortedList sl = new SortedList();
for (int i = 0; i < str.Length; i++)
{
if (sl.ContainsKey(str[i]))
{
int num = (int)sl[str[i]];
num++;
sl[str[i]] = num;
}
else
{
sl.Add(str[i], 1);
}
}
int temp = (int)sl.GetByIndex(0);
char abc = (char)sl.GetKey(0);
for (int i = 1; i < sl.Count; i++)
{
int count = (int)sl.GetByIndex(i);
if (count > temp)
{
temp = count;
abc = (char)sl.GetKey(i);
}
else if(count == temp)
{

}
else
{}
}
alpha.Add(abc.ToString());

char xyz;
for (int i = 0; i < sl.Count; i++)
{
int count = (int)sl.GetByIndex(i);
if (count == temp)
{
xyz = (char)sl.GetKey(i);
if(abc != xyz)
alpha.Add(xyz.ToString());
}
}
return temp;
}
llt_2006 2006-06-23
  • 打赏
  • 举报
回复
string str = "asdffdadfsdfdaddeef";
char[] chars = str.ToCharArray();
int n1 = 1;
int sum = 0;
char result = ' ';
for (int i = 0; i < chars.Length; i++)
{
for (int j = i + 1; j < chars.Length; j++)
{

if (chars[i].ToString() == str.Substring(j,1))
{
n1++;
}
}
if (n1 > sum)
{
sum = n1;
result = chars[i];
}
n1 = 1;
}

TextBox3.Text=result.ToString();
TextBox4.Text=sum.ToString();
调试OK
刘建 2006-06-23
  • 打赏
  • 举报
回复
用hastabe
可以很快搞定.
svevx 2006-06-23
  • 打赏
  • 举报
回复
Array.Sort(ori);
int sum = 1, pos = 0, max = 0;
for (int i = 1; i < ori.Length; i++)
{
if (ori[i - 1] == ori[i])
sum++;
else if (sum > max)
{
max = sum;
sum = 1;
pos = i - 1;
}
else
{
sum = 1;
}

}
this.label1.Text += "the max char is " + ori[pos] + " and the count is " + max + "\r\n";

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧