面视题

fireworksloveyou 2007-05-24 04:43:23
(1)输入一个字符串!求这个字符串中每个字符出现的个数
(2) 输入一串数字以逗号的形式隔开!求这个字符串数字的平均数,同时找出,与这个平均最相近的数字
...全文
1124 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
飘_飘 2007-05-25
  • 打赏
  • 举报
回复
string num = TextBox1.Text;
string[] a = num.Split(',');
int count =0;
double sum = 0;

foreach(string n in a)
{
sum =sum + double.Parse(n);
count ++;
}
double avg = sum/count;//平均值
double abs =Math.Abs(double.Parse(a[0])-avg);
double jiejin = 0;//最近值
foreach(string n in a)
{
if(Math.Abs(double.Parse(n)-avg)<abs)
{
abs = Math.Abs(double.Parse(n)-avg);
jiejin = double.Parse(n);
}
}
if(jiejin==0)
{
jiejin = double.Parse(a[0]);
}
sunqiso 2007-05-25
  • 打赏
  • 举报
回复
群号:23207804

C#梦之旅只要你喜欢 C#,并且热爱专研技术,那就加入我们的团队吧!!
lv810 2007-05-25
  • 打赏
  • 举报
回复
LZ该去C/C++区
那里这样类型的题多
ilove8 2007-05-25
  • 打赏
  • 举报
回复
up
liuyun1987 2007-05-25
  • 打赏
  • 举报
回复
高手...
esafe 2007-05-25
  • 打赏
  • 举报
回复
第一题:

string s = "ababcdefaceAVAXC";
char[] ch = new char[s.Length];
int [] ascii = new int [52];
s.CopyTo(0, ch, 0, s.Length);
foreach (char c in ch)
{
ascii[(int)c - 65]++;
}

for(int i=0;i<52;i++)
{
if (ascii[i] != 0) Console.WriteLine(((char)(i + 65)).ToString() +":"+ ascii[i]);
}
xiangyuen 2007-05-25
  • 打赏
  • 举报
回复
留个名
coolpc 2007-05-24
  • 打赏
  • 举报
回复
把字符串存在数组里面,在来操作……
LeoMaya 2007-05-24
  • 打赏
  • 举报
回复
private Dictionary<string, int> CountLetters(string line)
{
Dictionary<string, int> ret = new Dictionary<string, int>();
char[] chars = line.ToCharArray();
foreach (char c in chars)
{
Regex regex = new Regex(c.ToString());
ret[c.ToString()] = regex.Matches(line).Count;
}

return ret;
}

private int AvgValue(string line, out int result)
{
float sum = 0;
int ret = 0;
result = 0;
string[] pieces = line.Split(',');
int len = pieces.Length;
foreach (string piece in pieces)
{
sum += float.Parse(piece);
}
ret = (int)(sum / (float)len);
float temp = sum / len;
pieces = temp.ToString().Split('.');
if (pieces.Length > 1)
{
char[] chars = pieces[1].ToCharArray();
if (int.Parse(chars[0].ToString()) > 4) result = ret + 1;
else result = ret;
}
else result = ret;

return ret;
}
trueboy 2007-05-24
  • 打赏
  • 举报
回复
用这个比较简单:
Console.Write("Enter a string: ");

string aString = Console.ReadLine();

Dictionary<char, int> aDictionary = new Dictionary<char, int>();

for (int i = 0; i < aString.Length; i++)
{
if (aDictionary.ContainsKey(aString[i]))
{
aDictionary[aString[i]]++;
}
else
{
aDictionary.Add(aString[i], 1);
}
}

foreach (char aChar in aDictionary.Keys)
{
Console.WriteLine("Char: " + aChar + ", Number: " + aDictionary[aChar]);
}
IT_zen 2007-05-24
  • 打赏
  • 举报
回复
留个名,以为好查找
Qim 2007-05-24
  • 打赏
  • 举报
回复
//刚才有点错.现在可以处理小数
private void Todo(string as1, string as2)
{
string s1 = as1;
string s2 = as2;
string sResult = "";
Hashtable ht = new Hashtable();

/////////
for (int i = 0; i < s1.Length; i++)
{
if(ht.ContainsKey(s1[i].ToString()))
{
int ii = (int)ht[s1[i].ToString()];
ht[s1[i].ToString()] = ii + 1;
}
else
{
ht.Add(s1[i].ToString(), 1);

}
}
IEnumerator ie = ht.GetEnumerator();
while (ie.MoveNext())
{
DictionaryEntry ee = (DictionaryEntry)ie.Current;
sResult += ee.Key .ToString() + ":" + ee.Value.ToString() + "\r\n";
}

string[] ss = s2.Split(',');
double iCount = 0;
double dAve = 0.0;
for (int i = 0; i < ss.Length; i++)
{
iCount += double.Parse(ss[i]);
}
//average
dAve = iCount / ss.Length;

double dP = double .MaxValue ;
double dN = 0.0;
for (int i = 0; i < ss.Length; i++)
{
double iTemp = (double)Math.Abs(double.Parse(ss[i]) - dAve);

if (iTemp < dP)
{
dP = iTemp;
dN = double.Parse(ss[i]);
}
}

sResult += "\r\n";
sResult += "average:" + dAve.ToString() + "\r\n";
sResult += "nearly:" + dN.ToString();

//
MessageBox.Show(sResult);

}

}
wt_jn 2007-05-24
  • 打赏
  • 举报
回复
每个字符就需要循环处理(foreach)。
如果不希望重复的字符出现用Hashtable判定比较方便
Qim 2007-05-24
  • 打赏
  • 举报
回复
//试试有错吗?
private void Todo(string as1, string as2)
{
string s1 = as1;
string s2 = as2;
string sResult = "";
Hashtable ht = new Hashtable();

/////////
for (int i = 0; i < s1.Length; i++)
{
if(ht.ContainsKey(s1[i].ToString()))
{
int ii = (int)ht[s1[i].ToString()];
ht[s1[i].ToString()] = ii + 1;
}
else
{
ht.Add(s1[i].ToString(), 1);

}
}
IEnumerator ie = ht.GetEnumerator();
while (ie.MoveNext())
{
DictionaryEntry ee = (DictionaryEntry)ie.Current;
sResult += ee.Key .ToString() + ":" + ee.Value.ToString() + "\r\n";
}

string[] ss = s2.Split(',');
int iCount = 0;
double dAve = 0.0;
for (int i = 0; i < ss.Length; i++)
{
iCount += int.Parse(ss[i]);
}
//average
dAve = iCount / ss.Length;

double dP = double .MaxValue ;

for (int i = 0; i < ss.Length; i++)
{
int iTemp = (int)Math.Abs(int.Parse(ss[i]) - dAve);

if (iTemp < dP)
{
dP = iTemp;
}
}

sResult += "\r\n";
sResult += "average:" + dAve.ToString() + "\r\n";
sResult += "nearly:" + dP.ToString();

//
MessageBox.Show(sResult);

}

}
wt_jn 2007-05-24
  • 打赏
  • 举报
回复
第一题:
Regex r = new Regex("ab");
string s = "ababcabc";
MessageBox.Show(r.Matches(s).Count.ToString());
he_8134 2007-05-24
  • 打赏
  • 举报
回复
class Program
{
static void Main(string[] args)
{
while (true)
{
string input = Console.ReadLine();
if (input.Length == 0) break;
List<LetterKey> result = new List<LetterKey>();
for (int i = 0; i < input.Length; i++)
{
LetterKey letterKey = new LetterKey(input[i]);
int index = result.IndexOf(letterKey);
if (index == -1)
{
result.Add(letterKey);
}
else
{
result[index].Count++;
}
}
foreach (LetterKey lk in result)
{
Console.WriteLine(lk);
}
}
}
}
class LetterKey {
public char Letter;
public int Count;
public LetterKey(char letter) {
Letter = letter;
Count = 1;
}
public override string ToString()
{
return string.Format("字符:{0},出现次数:{1}.", Letter, Count);
}
public override bool Equals(object obj)
{
LetterKey eqObj = (LetterKey)obj;
return this.Letter.Equals(eqObj.Letter);
}
public override int GetHashCode()
{
return this.Letter.GetHashCode();
}
}
amandag 2007-05-24
  • 打赏
  • 举报
回复
做出来很容易 ...
还要看算法 ...
noky 2007-05-24
  • 打赏
  • 举报
回复
以上是第二问的解答,下面是的一问的解答
private void FindCharAverage()
{
char[] tmpChar = tmpString.ToCharArray();
Dictionary<char, int> tmpDictionary = new Dictionary<char, int>();
foreach (char ch in tmpChar)
{
if (!tmpDictionary.ContainsKey(ch))
{
tmpDictionary[ch] = 1;
}
else
{
tmpDictionary[ch] = tmpDictionary[ch] + 1;
}
}
}
noky 2007-05-24
  • 打赏
  • 举报
回复
string tmpString = "wo ai mm!mm ye ai wo ";
string tmpString1 = "22,33,567,36,90,1,4,70,60,55,33,79";

private void SpliterString()
{
int number = 0;
int sumNumber = 0;
int average = 0;
int FindInt = 0;
string [] tmpStrArray = tmpString1.Split(',');
List<int> tmpIntList = new List<int>();
foreach (string str in tmpStrArray)
{
number = Convert.ToInt32(str);
sumNumber += number;
tmpIntList.Add(number);
}
average = sumNumber / tmpIntList.Count;
FindInt = DichotomyFindInt(average, tmpIntList);
int i = 0;
}
private int DichotomyFindInt(int average, List<int> tmpList)
{
int startIndex = 0;
int endIndex = tmpList.Count - 1;
int mdiIndex = endIndex;
int mFindInt = 0;
if (endIndex <= 0)
return -1;
while (startIndex < endIndex)
{
mdiIndex = (startIndex + endIndex) / 2;
mFindInt = tmpList[mdiIndex];
if (average < mFindInt)
{
endIndex = mdiIndex - 1;
}
else if (average > mFindInt)
{
startIndex = mdiIndex + 1;
}
else
{
return mFindInt;
}
}
if (endIndex >= 0 && endIndex < tmpList.Count)
return tmpList[endIndex];
else
return -1;
}
编译环境 VS2005。使用环境WForm,已经通过测试.只作参考.
babyrockxray 2007-05-24
  • 打赏
  • 举报
回复
下班了,回家看看
加载更多回复(15)

110,499

社区成员

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

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

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