111,097
社区成员




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();
}
}
}
int[] arry = new int[12] { 1, 2, 3, 4, 5, 8, 9, 23, 24, 25,1000,1001 };
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];
}
}