如何查找一个字符串中重复次数最多的字符,返回这个字符的个数和字符
解决方案 »
- 关于两个treeview控件双向选中的问题
- 怎么样把多选框中选中的ID传到数据库中
- *****请问UserControl中的UserControl_Load()方法什么时候被调用的?
- 如何把C#编写的程序编译成象C++一样能不用.NET框架就可以运行的程序.
- 大神们,救命啊。 TFS没签入点了撤销更改了。
- 求高手,通过程序访问 打开的excel 并 编辑它
- 水晶报表有关参数的问题?
- 压缩后缀为001,002……的文件如何打开?
- form的简单问题,请教!
- 怎样在单击表单时 让表单上处于输入状态的TextBox发生Leave事件?
- Winform中如何判断Datagrid正在显示的是主表还是子表?
- 关于在windows窗体里运行脚本查询的问题
using System.Collections.Generic;
using System.Text;class Program
{
private static SortedDictionary<char, int> sd = new SortedDictionary<char, int>();
static void Main(string[] args)
{
string str = "1232143546534135164161";
foreach(char c in str)
{
Check(c);
}
char maxChar = Char.MinValue;
int maxValue = 0;
foreach(KeyValuePair<char, int> kvp in sd)
{
Console.WriteLine("Dictionary has a Key : {0} and Value: {1}", kvp.Key, kvp.Value);
if(kvp.Value > maxValue)
{
maxValue = kvp.Value;
maxChar = kvp.Key;
}
}
Console.WriteLine("Max char is {0}, times is {1}.", maxChar, maxValue);
Console.ReadLine();
}
private static void Check(char c)
{
if(sd.ContainsKey(c))//有了
{
sd[c]++;
}
else
{
sd.Add(c, 1);
}
}
}
string s = "ghjajikdnkxxxxfvnjxxzkxxxnfjkdmfn";
list.AddRange(s.ToCharArray());
list.Sort(); char c=' ';
int temp = 1;
char maxChar=' ';
int maxCount=0; for (int i = 0; i < list.Count; i++)
{
if (i > 0)
{
if (list[i] == c)
{
temp++;
}
else
{
temp = 1;
}
} if (temp > maxCount)
{
maxCount = temp;
maxChar = list[i];
} c = list[i];
}
Console.WriteLine(maxChar.ToString()+" "+maxCount.ToString());
顺:www.babykey.com
int intMax = 0;
int intTemp = 0;
foreach(char c in str)
{
intTemp = GetNum(str,c);
if ( intTemp > intMax )
{
intMax = intTemp;
chrMax = c;
}
}
{
string []strTemp;
strTemp = str1.Split(c);
return strTemp.Length-1;
}
char[] c = ss.ToCharArray();
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
int i = 0;
int k = 0;
foreach ( char cc in c)
{
if (!al1.Contains(cc))
{
al1.Add(cc);
al2.Add(1);
}
else
{
al2.Insert(al1.IndexOf(cc),Convert.ToInt32(al2[al1.IndexOf(cc)].ToString()) + 1);
al2.RemoveAt(al1.IndexOf(cc) + 1);
}
}
for ( int j=0;j<al2.Count;j++ )
{
if ( Convert.ToInt32(al2[j].ToString()) > i)
{
i = Convert.ToInt32(al2[j].ToString());
k = j;
}
}
string s = "字符" + al1[k].ToString() + "出现" + i.ToString() + "次";
StringBuilder sb = new StringBuilder(s);
List<int> charCount = new List<int>();
List<char> lchar = new List<char>();
int i, j;
i = 0; j = 1;
while (sb.Length > 0 && i<sb.Length)
{
charCount.Add(1);
lchar.Add(sb[0]);
while (sb.Length > 0 && j<sb.Length)
{
if (sb[0] == sb[j])
charCount[i]++;
j++;
}
sb.Replace(sb[0].ToString(), "");
i++;
j = 1;
}
for (int k = 0; k < charCount.ToArray().Length; k++)
{
Console.WriteLine(string.Format("{0,4} {1}", lchar[k].ToString(), charCount[k]));
}
Console.ReadLine();