取连续的数据,要记录连续数据的开头和结尾

rainfull 2010-09-08 05:11:05
现在有一组数据,是断断续续的数据,我现在有取连续的数据,要记录连续数据的开头和结尾

1
2
3
4
8
9
10
13
14
15
16
17

我要把这些连续的数据分段,第一段1-4,第二段8-10,第三段10-17

...全文
163 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainfull 2010-09-09
  • 打赏
  • 举报
回复
要剔除不是连续的
rainfull 2010-09-09
  • 打赏
  • 举报
回复
现在再问一个问题,如果我把这个数组改一下,1,3,4,5,6,9,10,11,12,14,16,17,18,19,25
我怎么将他们断开列!
rainfull 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 spx427 的回复:]
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……
[/Quote]

现在再问一个问题,如果我把这个数组改一下,1,3,4,5,6,9,10,11,12,16,17,18,19,25
我怎么将他们断开列!
gongsun 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jointan 的回复:]
你凭什么把它们分为:
1-4,8-10,10-17

没看出规律来
[/Quote]

应该是 1-4,8-10,13-17

lz写错了。

按照 递增+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]=4

a[1][0]=8
a[1][1]=9
a[1][2]=10

...

spx427 2010-09-09
  • 打赏
  • 举报
回复
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]);
}
}

}

开头和结尾分别记在start和end两个集合里面了。
kensouterry 2010-09-09
  • 打赏
  • 举报
回复
一个控制台,不连续的懒得写了

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();
}

}
}
jsjhj2008 2010-09-09
  • 打赏
  • 举报
回复
不对吗?详细点说说呢,举个例子
rainfull 2010-09-09
  • 打赏
  • 举报
回复
int[] arry = new int[12] { 1, 3, 4, 5, 8, 9,13, 23, 24, 25,30,1000,1001 };

把不是连续的剔除来

rainfull 2010-09-09
  • 打赏
  • 举报
回复
int[] arry = new int[12] { 1, 3, 4, 5, 8, 9,13, 23, 24, 25,30,1000,1001 };

把是连续的剔除来
jsjhj2008 2010-09-09
  • 打赏
  • 举报
回复
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];
}
}

测试通过了,楼书试试
jointan 2010-09-08
  • 打赏
  • 举报
回复
你凭什么把它们分为:
1-4,8-10,10-17

没看出规律来

rainfull 2010-09-08
  • 打赏
  • 举报
回复
给点思路,第一个好判断,到第二组数据怎么办

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧