简单的处理思路就是把这些数字放到一个key-value的dictionary中,key存放位置,value存放具体数据,接下来就是写程序判断是否连续了。
解决方案 »
- 带有SQL2005的C#项目怎么样移植到别的电脑上使用,或者说带有数据库的项目怎么进行安装打包
- C#中查询后datagridview显示查询的结果
- 如何让Button 的Click事件自动执行
- 数据库常用连接字符串
- 怎样用.NET Framework 2.0的DataGridView控件绑定DataSet的内容?
- winform怎么获取移动硬盘或U盘的盘符
- 怎样在MouseMove 和 Drag 之间切换
- Windows应用程序界面问题
- 如何给类创建自己的属性管理器?
- 如何实现在TreeNode上按滑鼠右键事件??
- TekTronix示波器 VISA 通讯ERROR
- 请问com组件操作WORD 插入图片一定是必须本地的吗 Selection.InlineShapes.AddPicture
string s1 = s.Substring(4, 5);
string s2 = s.Substring(13, 5);
string s = "0,2,5,6,7,10,12,13,15,18,21,24.....";
string f = "5,6,7";
int i=s.IndexOf(f); //起始位置
int j = i + f.Length;//结束位置
代码是VB的,你看看思路对不对吧。
Dim arr = New Integer() {0, 2, 5, 6, 7, 10, 12, 13, 15, 18}
Dim IdxStart As Integer = -1
Dim IdxEnd As Integer = -1 For i As Integer = 0 To arr.Length - 2
If arr(i) + 1 = arr(i + 1) Then
If IdxStart = -1 Then
IdxStart = i
End If
IdxEnd = i + 1
Else
If IdxStart <> -1 Then
Console.WriteLine(String.Format("({0},{1})", IdxStart, IdxEnd))
IdxStart = -1
End If
End If
Next
If IdxStart <> -1 Then
Console.WriteLine(String.Format("({0},{1})", IdxStart, IdxEnd))
End If
如果讨厌在循环外面还要做处理的话,可以这样做。
Dim arr = New Integer() {0, 2, 5, 6, 7, 10, 12, 13, 15, 18}
Dim IdxStart As Integer = -1
Dim IdxEnd As Integer = -1 Dim lst = arr.ToList
lst.Add(-1) '在最后增加一个绝不会连续的数字, For i As Integer = 0 To lst.Count - 2
If lst(i) + 1 = lst(i + 1) Then
If IdxStart = -1 Then
IdxStart = i
End If
IdxEnd = i + 1
Else
If IdxStart <> -1 Then
Console.WriteLine(String.Format("({0},{1})", IdxStart, IdxEnd))
IdxStart = -1
End If
End If
Next
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] data = { 0, 2, 5, 6, 7, 10, 12, 13, 15, 18 };
List<Tuple<int, int>> result = new List<Tuple<int, int>>();
int pre = data[0];
int startidx = 0;
bool incapture = false;
for (int i = 1; i < data.Length; i++)
{
if (pre + 1 == data[i] && !incapture)
{
incapture = true;
startidx = i - 1;
}
if (pre + 1 != data[i] && incapture)
{
incapture = false;
result.Add(new Tuple<int, int>(startidx, i - 1));
}
pre = data[i];
}
foreach (var item in result)
Console.WriteLine("{0} ~ {1}", item.Item1 + 1, item.Item2 + 1);
}
}
}
3 ~ 5
7 ~ 8
请按任意键继续. . .
string result = data.Aggregate(new { Lists = Enumerable.Empty<IEnumerable<int>>(), LastData = null as int?, LastIndex = 0 }, (s, cur) => s.LastData == null ? new { Lists = s.Lists.Concat(Enumerable.Repeat(Enumerable.Repeat(1, 1), 1)).ToArray().AsEnumerable(), LastData = cur as int?, LastIndex = 1 } : s.LastData + 1 == cur ? new { Lists = s.Lists.Select(e => e != s.Lists.Last() ? e : e.Concat(Enumerable.Repeat(s.LastIndex + 1, 1)).ToArray()).ToArray().AsEnumerable(), LastData = cur as int?, LastIndex = s.LastIndex + 1 } : new { Lists = s.Lists.Concat(Enumerable.Repeat(Enumerable.Repeat(s.LastIndex + 1, 1), 1)).ToArray().AsEnumerable(), LastData = cur as int?, LastIndex = s.LastIndex + 1 }).Lists.Where(l => l.Count() > 1).Select(l => String.Format("{0}->{1}", l.First(), l.Last())).Aggregate((a, b) => a + Environment.NewLine + b);
自从学了F#,写的代码都像这样了……
很久以前,我也用Linq回答过这个问题,被批过代码可读性差。
http://bbs.csdn.net/topics/390368137
很久以前,我也用Linq回答过这个问题,被批过代码可读性差。
http://bbs.csdn.net/topics/390368137
这是F#改来的:
let rec aggrator listLeft lastIndex lastData startIndex accret =
match listLeft with
| [] when startIndex = lastIndex -> accret
| [] -> accret @ [( startIndex , lastIndex )]
| (cur & head) :: tail when cur = lastData + 1 -> aggrator tail (lastIndex + 1) cur startIndex accret
| head :: tail when lastIndex <> startIndex -> aggrator tail (lastIndex + 1) head (lastIndex + 1) (accret @ [( startIndex , lastIndex )])
| head :: tail -> aggrator tail (lastIndex + 1) head (lastIndex + 1) accretlet aggr l = aggrator l 0 l.Head 0 []
{
int[] data = { 0, 2, 5, 6, 7, 10, 12, 13, 15, 18 }; // 构造一个队列,带上前后元素的值,i:下标,v:元素值,a:前一个值,b:后一个值
var magic = new []{int.MaxValue};
var q = magic.Concat(data).Concat(magic);
var q1 = q.Zip(q.Skip(1), (a, v)=>new {v,a})
.Zip(q.Skip(2), (x, b)=>new{x.v, x.a, b})
.Select((x, i) => new {i, x.v, x.a, x.b}); // 选出序列的起始元素
var qx = q1.Where(x=> x.v-x.a != 1 && x.b-x.v == 1);
// 选出序列的结束元素
var qy = q1.Where(x=> x.v-x.a == 1 && x.b-x.v != 1);
// 拼合成想要的下标值对
var result = qx.Zip(qy, (x,y)=>new{x=x.i, y=y.i}); foreach (var x in result)
{
Console.WriteLine("{0}~{1}", x.x, x.y);
}
}
很久以前,我也用Linq回答过这个问题,被批过代码可读性差。
http://bbs.csdn.net/topics/390368137
这是F#改来的:
let rec aggrator listLeft lastIndex lastData startIndex accret =
match listLeft with
| [] when startIndex = lastIndex -> accret
| [] -> accret @ [( startIndex , lastIndex )]
| (cur & head) :: tail when cur = lastData + 1 -> aggrator tail (lastIndex + 1) cur startIndex accret
| head :: tail when lastIndex <> startIndex -> aggrator tail (lastIndex + 1) head (lastIndex + 1) (accret @ [( startIndex , lastIndex )])
| head :: tail -> aggrator tail (lastIndex + 1) head (lastIndex + 1) accretlet aggr l = aggrator l 0 l.Head 0 []嗯,F#的模式匹配写起来很优雅。赞一个
{
int[] data = { 0, 2, 5, 6, 7, 10, 12, 13, 15, 18 }; var q = data.Select((v,i)=>new {i, v});
var result = q.Where((x,i)=> (i==0 || x.v-data[i-1] != 1) && i<data.Length-1 && data[i+1]-x.v == 1)
.Zip(q.Where((x,i)=> i>0 && x.v-data[i-1] == 1 && (i==data.Length-1 || data[i+1]-x.v != 1)),
(x,y)=>new{x=x.i, y=y.i});
foreach (var x in result)
{
Console.WriteLine("{0}~{1}", x.x, x.y);
}
}
using System.Collections.Generic;
using System.Linq;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var arr = new int[] { 0, 2, 5, 6, 7, 10, 12, 13, 15, 18, 19, 21, 22, 23, 24, 25, 17, 29, 30 };
var result = 查找连续数字(arr).ToList();
foreach (var r in result)
Console.Write("({0},{1}) ", r.Item1, r.Item2);
Console.WriteLine("\r\n其中长度不小于3的是:");
var query = from r in result
where r.Item2 - r.Item1 + 1 >= 3
select r;
foreach (var r in query)
Console.Write("({0},{1}) ", r.Item1, r.Item2);
Console.Write("\r\n按任意键结束......");
Console.ReadKey();
} private static IEnumerable<Tuple<int, int>> 查找连续数字(int[] arr)
{
var start = 0;
while (start < arr.Length - 1)
{
var end = 查找结束位置(arr, start);
if (end != start)
yield return new Tuple<int, int>(start + 1, end + 1);
start = end + 1;
}
} private static int 查找结束位置(int[] arr, int start)
{
for (var j = start + 1; j < arr.Length; j++)
if (arr[j - 1] + 1 != arr[j])
return j - 1;
return arr.Length - 1;
} }
}
做一个结构,保存 a,a+1 的差值
结果就会类似 2,2,-4,1,1,1,0,-9任何连续的1 就是你要的东西了至于语法,一直没兴起学习新东西,一直用简单的 for 好了各位谈论的语法,包括 Linq在内,是不是 在 原始实现阶段也是各种for? 我没细心看过,不对请喷我,我就知道了
int t = 0;
string aa = "";
bool bb = false;
do
{
if (data[t + 1] - data[t] == 1)
{
if (!bb)
aa += t.ToString() + ",";
bb = true;
}
else
if (bb)
{
aa += t.ToString() + ";";
bb = false;
}
} while (++t < data.Length - 2);
if (bb)
aa += (t + 1).ToString() + ";";做个按思路简单的写法应该比较好理解
差不多。这是编程语言课程上的题目。编程语言在软件专业来说,不算是任何一种很高级的基础理论课,只能算是一种职业技能。软件专业有几十门基础理论课,那些课程中的题目才算是“大学题目”。如果这这可题目有什么“大学题目”内容,让我来说,我觉得决策使用中间数据结构 Tuple<int,int> 是个体现了程序素质的考试点(首先考虑在选择数据结构上尽量规范和可扩展),这可以看出程序员在写一个程序时其实内心里边已经有了想为10倍的“将来应用”留余地的打算。
//上面数组中的每一项和它的索引相减会得到一个值
//如果值是连续的,那么和索引相减得到的值是相同的,这个值成为分组因子
//下面的Select是为了获取每一项的索引,因为GroupBy无法获取每一项的索引
var gp = rdn.Select((num, index) => new { Num = num, Index = index })
.GroupBy(pair => pair.Num - pair.Index); foreach (var item in gp)
{
if (item.Count() <= 1)
continue; //最小索引
Console.Write("{{{0}, ",item.Min(pair => pair.Index));
//最大索引
Console.Write("{0}}}",item.Max(pair=>pair.Index)); Console.WriteLine();
}
相关处可见《Microsoft SQL SERVER 2005技术内幕:T-SQL查询》的"数字辅助表,已有范围"章节
差不多。这是编程语言课程上的题目。编程语言在软件专业来说,不算是任何一种很高级的基础理论课,只能算是一种职业技能。软件专业有几十门基础理论课,那些课程中的题目才算是“大学题目”。如果这这可题目有什么“大学题目”内容,让我来说,我觉得决策使用中间数据结构 Tuple<int,int> 是个体现了程序素质的考试点(首先考虑在选择数据结构上尽量规范和可扩展),这可以看出程序员在写一个程序时其实内心里边已经有了想为10倍的“将来应用”留余地的打算。
剖析得很专业哦~