public int[] data= {1,2,3,4,8,9,10,13,14,15,16,17}; public List<int> start = new List<int>(); public List<int> end = new List<int>(); public void method() { start.Add(data[0]); for (int i = 0; i < data.Length - 1;i++ ) {
if (data[i + 1] - data[i] != 1) { end.Add(data[i]); start.Add(data[i + 1]); } if (i == data.Length - 2) { end.Add(data[i+1]); } }
一个控制台,不连续的懒得写了using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data;namespace Temp { class Program { int[] array = new int[13] { 1, 3, 4, 5, 8, 9, 13, 23, 24, 25, 30, 1000, 1001 };
List<int> begin = new List<int>(); List<int> end = new List<int>(); //The method only satisfy increased sequence number. public void GetContinuousSequence(int[] array) { int k = 0; int times = 0; for (int i = 0; i < array.Length; i++) { //Judging the last sequence. if (i == array.Length - 1) { begin.Add(k + 1 - times); end.Add(k + 1); break; } //the number will be set as a sub-continuous-sequence number's beginning. if (array[i] == array[i + 1] - 1) { k = array[i]; times++; } if ((array[i] != array[i + 1] - 1)&& times!=0) { begin.Add(k + 1 - times); end.Add(k + 1); times = 0; } } //Now,you can get serveral pairs exist in List begin and List end,which records continuous sequence's beginning number and end number at the same index. List<int> sequenceList = ExtendSequenceNumber(begin, end); //Display sequence number. for (int i = 0; i < sequenceList.Count; i++) { Console.WriteLine("{0},", sequenceList[i]); } } private List<int> ExtendSequenceNumber(List<int> begin, List<int> end) { List<int> listSequence = new List<int>(); for (int i = 0; i < begin.Count; i++) { for (int j = begin[i]; j < end[i]+1; j++) { listSequence.Add(j); } } return listSequence; } static void Main(string[] args) { Program p1 = new Program(); p1.GetContinuousSequence(p1.array); Console.WriteLine("finish..."); Console.ReadLine(); } } }
public List<int> start = new List<int>();
public List<int> end = new List<int>(); public void method()
{
start.Add(data[0]);
for (int i = 0; i < data.Length - 1;i++ )
{
if (data[i + 1] - data[i] != 1)
{
end.Add(data[i]);
start.Add(data[i + 1]);
}
if (i == data.Length - 2)
{
end.Add(data[i+1]);
}
}
} 开头和结尾分别记在start和end两个集合里面了。
应该是 1-4,8-10,13-17lz写错了。按照 递增+1 先 int m[] 装你所有的数在 循环 去判断...(m[i=1] - m[i] 是否=1 )再搞一个2维数组装 就可以了a[0][0]=1
a[0][1]=2
a[0][2]=3
a[0][3]=4a[1][0]=8
a[1][1]=9
a[1][2]=10...
int flag = 1;
for (int i = 0; i < arry.Length - 1; i++)
{
if (i == arry.Length - 2)
{
Response.Write("第" + flag.ToString() + "到" + arry[i + 1].ToString() + "</br>");
}
else if (arry[i] + 1 != arry[i + 1])
{
Response.Write("第" + flag.ToString() + "到" + arry[i].ToString() + "</br>");
flag = arry[i + 1];
}
}
测试通过了,楼书试试
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace Temp
{
class Program
{
int[] array = new int[13] { 1, 3, 4, 5, 8, 9, 13, 23, 24, 25, 30, 1000, 1001 };
List<int> begin = new List<int>();
List<int> end = new List<int>(); //The method only satisfy increased sequence number.
public void GetContinuousSequence(int[] array)
{
int k = 0;
int times = 0;
for (int i = 0; i < array.Length; i++)
{
//Judging the last sequence.
if (i == array.Length - 1)
{
begin.Add(k + 1 - times);
end.Add(k + 1);
break;
}
//the number will be set as a sub-continuous-sequence number's beginning.
if (array[i] == array[i + 1] - 1)
{
k = array[i];
times++;
}
if ((array[i] != array[i + 1] - 1)&& times!=0)
{
begin.Add(k + 1 - times);
end.Add(k + 1);
times = 0;
}
}
//Now,you can get serveral pairs exist in List begin and List end,which records continuous sequence's beginning number and end number at the same index.
List<int> sequenceList = ExtendSequenceNumber(begin, end); //Display sequence number.
for (int i = 0; i < sequenceList.Count; i++)
{
Console.WriteLine("{0},", sequenceList[i]);
}
} private List<int> ExtendSequenceNumber(List<int> begin, List<int> end)
{
List<int> listSequence = new List<int>();
for (int i = 0; i < begin.Count; i++)
{
for (int j = begin[i]; j < end[i]+1; j++)
{
listSequence.Add(j);
}
}
return listSequence;
} static void Main(string[] args)
{
Program p1 = new Program();
p1.GetContinuousSequence(p1.array); Console.WriteLine("finish...");
Console.ReadLine();
} }
}