如
28,29,30,35,36,37,87,88,101,102,103,104,105,106
以上是一串数字,数字连续在一起的分成一组,共有5组
请教高手如何分组成(28,29,30) (35,36,37) (87,88) (101,102,103,104,105,106)这样的5组
再取每组的中间值
28,29,30,35,36,37,87,88,101,102,103,104,105,106
以上是一串数字,数字连续在一起的分成一组,共有5组
请教高手如何分组成(28,29,30) (35,36,37) (87,88) (101,102,103,104,105,106)这样的5组
再取每组的中间值
--------------------------------------------
数据串是本身自动排列好的
28,29,30,35,36,37,87,88,101,102,103,104,105,106
以上是一串数字,数字连续在一起的分成一组
(28,29,30) (35,36,37) (87,88) (101,102,103,104,105,106)
//.net 3.5以上
//若是无序数组,需要自己先排序
//需要引用命名空间 System.Linq
string source = "28,29,30,35,36,37,87,88,101,102,103,104,105,106";
string[] result = source.Split(',');//转换成int数组
int[] numbers = new int[result.Length];
for (int i = 0; i < result.Length; i++ )
{
numbers[i] = Convert.ToInt32(result[i]);
}List<List<int>> list = new List<List<int>>(); //存放分组int previousValue = -1; //记录前一个值,以便和当前值比较
List<int> group = null; //保存一个分组
for (int i = 0; i < numbers.Length; i++)
{
if (previousValue != numbers[i] - 1) //与前一个数字不连续,需要重新分组
{
if (group != null)
{
list.Add(group); //保存之前的组
}
group = new List<int>();
} //记录当前值并添加到当前分组中
previousValue = numbers[i];
group.Add(numbers[i]); //最后一组特殊处理
if (i == numbers.Length - 1)
{
list.Add(group);
}
}foreach (List<int> item in list)
{
Response.Write("<br />");
foreach (int value in item)
{
Response.Write( value + " ");
} Response.Write(" 中间值:" + item[item.Count / 2]);
}
/*
28 29 30 中间值:29
35 36 37 中间值:36
87 88 中间值:88
101 102 103 104 105 106 中间值:104
*/
{
string _strChar = "28,29,30,35,36,37,87,88,101,102,103,104,105,106";
ArrayList _List = new ArrayList(); string[] _IntList = _strChar.Split(',');
string[] _cIntList = _IntList.ToArray(); ArrayList _Inserted = new ArrayList();
foreach (string _Int in _IntList)
{
if (_Inserted.Contains(_Int)) continue;
int _TInt = Convert.ToInt32(_Int);
int _iMin = _TInt - (_TInt % 10) + 1;
int _uMax = _TInt + (10 - (_TInt % 10));
ArrayList _strList = new ArrayList();
_strList.Add(_Int);
_Inserted.Add(_Int);
foreach (string _CInt in _cIntList)
{ if (Convert.ToInt32(_CInt) >= _iMin && Convert.ToInt32(_CInt) <= _uMax)
{
if (!_strList.Contains(_CInt))
{
_strList.Add(_CInt);
_Inserted.Add(_CInt);
}
}
}
_List.Add(_strList);
} foreach (ArrayList _al in _List)
{
Console.WriteLine(_al.ToString());
}
Console.ReadLine();
}
List<List<int>> _List = new List<List<int>>(); string[] _IntList = _strChar.Split(',');
string[] _cIntList = _IntList.ToArray(); List<int> _Inserted = new List<int>();
foreach (string _Int in _IntList)
{
if (_Inserted.Contains(Convert.ToInt32(_Int))) continue;
int _TInt = Convert.ToInt32(_Int);
int _iMin = _TInt - (_TInt % 10) + 1;
int _uMax = _TInt + (10 - (_TInt % 10));
List<int> _strList = new List<int>();
_strList.Add(Convert.ToInt32(_Int));
_Inserted.Add(Convert.ToInt32(_Int));
foreach (string _CInt in _cIntList)
{ if (Convert.ToInt32(_CInt) >= _iMin && Convert.ToInt32(_CInt) <= _uMax)
{
if (!_strList.Contains(Convert.ToInt32(_CInt)))
{
_strList.Add(Convert.ToInt32(_CInt));
_Inserted.Add(Convert.ToInt32(_CInt));
}
}
}
_List.Add(_strList); Console.WriteLine("数组中间值为:" + _strList[_strList.Count/2]);
}
Console.ReadLine();
{
string str = "28,29,30,35,36,37,87,88,101,102,103,104,105,106";
List<List<int>> list = new List<List<int>>();
str.Split(',').OrderBy(x => Convert.ToInt32(x)).Aggregate((x, y) =>
{
if (list.Count == 0)
list.Add(new List<int> { Convert.ToInt32(x) });
else if (list.Last().Last() + 1 == Convert.ToInt32(x))
list.Last().Add(Convert.ToInt32(x));
else
list.Add(new List<int> { Convert.ToInt32(x) });
return y;
});
foreach (List<int> l in list)
{
foreach (int i in l)
{
Console.Write(i.ToString().PadLeft(5));
}
Console.Write("中间值:" + l[l.Count / 2]);
Console.WriteLine();
}
Console.ReadLine();
}
/*
28 29 30 中间值:29
35 36 37 中间值:36
87 88 中间值:88
101 102 103 104 105 106 中间值:103
*/