写段程序统计一个字符串中重复字符出现的次数

renzaijiang 2009-02-28 09:13:59
注意可能很长的字符哦也不是全是字母 考虑下算法效率哦
输入:

aaacccbbb33322145@#$

输出:
a 3
c 3
b 3
3 3
2 2
...全文
181 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengfong 2009-03-01
  • 打赏
  • 举报
回复
学这个吧:
http://www.soasp.net/CShape/
chengfong 2009-03-01
  • 打赏
  • 举报
回复
学这个吧:
http://www.soasp.net/CShape/
icehawk 2009-02-28
  • 打赏
  • 举报
回复
巧妙,学到一招,哈哈
成都慢生活 2009-02-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyi8808 的回复:]
C# codeusing System;
using System.Collections.Generic;

class A
{
static void Main()
{
string s = "aaacccbbb33322145@#$";
Dictionary<char,int> count = GetCharCount(s);
foreach (KeyValuePair<char,int> kvp in count)
{
if (kvp.Value > 1) Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
}
}

static Dictionary<char,int> GetCharCount(string text)

[/Quote]
很炫
春天的气息 2009-02-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyi8808 的回复:]
C# codeusing System;
using System.Collections.Generic;

class A
{
static void Main()
{
string s = "aaacccbbb33322145@#$";
Dictionary<char,int> count = GetCharCount(s);
foreach (KeyValuePair<char,int> kvp in count)
{
if (kvp.Value > 1) Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
}
}

static Dictionary<char,int> GetCharCount(string text)

[/Quote]

算法不错,
wuyi8808 2009-02-28
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;

class A
{
static void Main()
{
string s = "aaacccbbb33322145@#$";
Dictionary<char,int> count = GetCharCount(s);
foreach (KeyValuePair<char,int> kvp in count)
{
if (kvp.Value > 1) Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
}
}

static Dictionary<char,int> GetCharCount(string text)
{
Dictionary<char,int> d = new Dictionary<char,int>();
foreach (char c in text)
{
int n;
d.TryGetValue(c, out n);
d[c] = n + 1;
}
return d;
}
}
/* 程序输出:
a: 3
c: 3
b: 3
3: 3
2: 2
*/
wuyi8808 2009-02-28
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;

class A
{
static void Main()
{
string s = "aaacccbbb33322145@#$";
Dictionary<char,int> count = GetCharCount(s);
foreach (KeyValuePair<char,int> kvp in count)
{
Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
}
}

static Dictionary<char,int> GetCharCount(string text)
{
Dictionary<char,int> d = new Dictionary<char,int>();
foreach (char c in text)
{
if (d.ContainsKey(c)) d[c]++;
else d[c] = 1;
}
return d;
}
}
/* 程序输出:
a: 3
c: 3
b: 3
3: 3
2: 2
1: 1
4: 1
5: 1
@: 1
#: 1
$: 1
*/
止戈而立 2009-02-28
  • 打赏
  • 举报
回复
		string GetCharCount(string text)
{
string result=string.Empty;
int len=text.Length;
while(len>0)
{
string temp=text[0].ToString();
text=text.Replace(temp,"");
int count=len-text.Length;
result+=temp+","+count+"\r\n";
len=text.Length;
}
return result;
}

110,533

社区成员

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

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

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