帮帮我!c#实现按字符串出现的次数排序(少到多)谢谢!!1
解决方案 »
- Microsoft.DirectX.Direct3D.Mesh 取顶点的问题
- 有关数据透视图在C#中实现的问题
- 关于控件treeview的问题
- (XML)序列化和反序列化的一个问题
- winform中textbox数字前面为0时会自动清除0
- 求。。。在html源代码中提取多个email地址的正则表达式
- 在VS2005用什么系统最好
- 请教 简单数据库查询问题(在线等,解决即结帖)
- 关于WMP.dll的问题
- vs2010c#winform水晶报表不能预览
- c#2003+ sql2000 字段数目超过100的表扔进设计器时出错,少于100字段的表扔进设计器又可以了, 新手求救宋江阿。
- vs2005安装问题
结果:this
is
ox
就这样啊!!帮帮我!!谢谢!!
{
string str = "is this is ox is ox is ox";
Hashtable ht = new Hashtable();
string[] words = str.Split(' ');
foreach (string word in words)
{
if (ht.Contains(word))
ht[word] = Convert.ToInt32(ht[word]) + 1;
else
ht[word] = 1;
}
List<int> lstValues = new List<int>();
List<string> lstKeys = new List<string>();
foreach (DictionaryEntry entry in ht)
{
lstValues.Add(Convert.ToInt32(entry.Value));
lstKeys.Add(entry.Key.ToString());
}
int[] values = lstValues.ToArray();
string[] keys = lstKeys.ToArray();
Array.Sort<int, string>(values, keys); foreach (string s in keys)
{
Console.WriteLine(s);
}
}觉得写得很烂
ArrayList str1=new ArrayList();//用来存放字符串中各字符
ArrayList int1=new ArrayList();//用来存放各字符出现的次数
string s=TextBox1.Text; //用来排序的字符串
while(s.Length!=0)
{
str1.Add(s.Substring(0,1));
int i=s.Length-s.Replace(s.Substring(0,1),"").Length;
int1.Add(i);
s=s.Replace(s.Substring(0,1),"");
}
for(int i=0;i<int1.Count;i++)
{
for(int j=0;j<int1.Count-1;j++)
{
if((int)int1[j]>(int)int1[j+1])
{
int temp=0;
temp=(int)int1[j];
int1[j]=int1[j+1];
int1[j+1]=(object)temp; string temp1="";
temp1=(string)str1[j];
str1[j]=str1[j+1];
str1[j+1]=(object)temp1;
}
}
}
for(int i=0;i<str1.Count;i++)
{
TextBox2.Text +=(string)str1[i]; //TextBox2.Text存放排序后的字符
}例如:在TextBox1.Text中输入“sdfdss” ,TextBox2.Text输出为:“fds”(f出现1次,d出现2次,s出现3次)
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;
using System.Collections;public class B
{
static void Main()
{
Console.Write("输入y开始,其他键退出...");
while ( Console.ReadKey().KeyChar == 'y')
{
Console.WriteLine();
Console.WriteLine("请输入语句:");
string input = Console.ReadLine();
string[] items = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
Hashtable ht = new Hashtable();
foreach (string s in items)
{
ht[s] = (ht[s] == null ? 1 : Convert.ToInt32(ht[s]) + 1);
}
object[] keys = new ArrayList(ht.Keys).ToArray();
object[] values = new ArrayList(ht.Values).ToArray();
Array.Sort(values, keys);
foreach (object o in keys)
{
Console.WriteLine(o.ToString());
}
Console.Write("输入Y开始,其他键退出...");
}
}
}