110,533
社区成员
发帖
与我相关
我的任务
分享
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
*/
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
*/
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;
}