比如,有这样一个数组 {1,1,1,2,1,1,2,1,1,1,1,1,2,2,2,0,2,2}输出为:1有3个,2有1个,1有2个,2有1个,1有2个,2有3个,0有1个,2有2个请问怎么做?写出具体代码最好,谢谢!
解决方案 »
- C#提取word带合并单元格表格
- C#下调用C++ DLL中函数
- TCP协议到底能实现P2P吗??
- 调用雅虎台湾的搜索引擎获取结果,但返回结果不正确(比较急)
- toolStrip控件上的按钮鼠标放上去的这个颜色能改吗?在哪里改?
- C#初学者 求 C/S模式 简单的日记应用 的整套开发设计思路
- 如何把已经保存在数据库中二进制数据转换为图片呢??
- 如何在某个控件的双击事件中判断是左键双击还是右键双击?
- Winform下的DataGrid的列的文本对齐问题
- Microsoft.Web.UI.WebControls的简单问题
- 运行程序报错Object reference not set to an instance of an object该怎么弄?
- C# 操作word速度好慢,谁能帮忙分析一下
static string ComputeNumber(int[] array)
{
StringBuilder builder = new StringBuilder(); int lastValue=-1;//记录上一个数
int lastNum = 0;//记录上一个数出现的次数
for (int n = 0; n < array.Length; n++)
{
if(n==0)
{
lastValue = array[n];
lastNum = 1;
}
else if (lastValue != array[n])//不同
{
builder.AppendFormat("{0}有{1},", lastValue, lastNum);
lastValue = array[n];
lastNum = 1;
}
else//相同
{
lastNum++;
}
}
if(lastNum!=0)
{
builder.AppendFormat("{0}有{1},",lastValue,lastNum);
}
return builder.ToString();
}
{
int cur;
int per;
int n=0;
for(int i=0;i<source.Length;++i)
{
cur=source[i];
per=i==0:source[0]:source[i-1];
if(cur==per)
{
++n;
}
else
{
Console.Out.WriteLine(String.Format("{0}有{1}个",per,n));
n=1;
per=cur;
}
Console.Out.WriteLine(String.Format("{0}有{1}个",cur,n));//输出最后一个
}
}
int[] array = { 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 0, 2, 2 };
int current = array[0], count = 0;
foreach (int i in array)
{
if (i != current)
{
Console.Write("{0}有{1}个,", current, count);
current = i;
count = 1;
}
else
count++;
}
Console.WriteLine("{0}有{1}个", current, count);
int[] array = { 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 0, 2, 2 };
List<int> list= array.ToList();
list.Sort();
list.ForEach(p => Console.WriteLine(p));
array=list.ToArray();
int current = array[0], count = 0;
foreach (int i in array)
{
if (i != current)
{
Console.Write("{0}有{1}个,", current, count);
current = i;
count = 1;
}
else
count++;
}
Console.WriteLine("{0}有{1}个", current, count);
var result = from a in array
group a by a into g
select new { g.Key, Num = g.Count() };
result.ToList().ForEach(p => Console.WriteLine("元素:{0}有{1}个", p.Key, p.Num));