简单的处理思路就是把这些数字放到一个key-value的dictionary中,key存放位置,value存放具体数据,接下来就是写程序判断是否连续了。

解决方案 »

  1.   

       string s = "0,2,5,6,7,10,12,13,15,18,21,24.....";
                string s1 = s.Substring(4, 5);
                string s2 = s.Substring(13, 5);
      

  2.   

    看错了:
     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;//结束位置
      

  3.   

    下标你用的是1开始的,我习惯用0开始的,也就是(2,4),(6,7)
    代码是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
      

  4.   

    using System;
    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
    请按任意键继续. . .
      

  5.   

    int[] data = { 0, 2, 5, 6, 7, 10, 12, 13, 15, 18 };
    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#,写的代码都像这样了……
      

  6.   


    很久以前,我也用Linq回答过这个问题,被批过代码可读性差。
    http://bbs.csdn.net/topics/390368137
      

  7.   


    很久以前,我也用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 []
      

  8.   

    用linq可读性也好吧。 static void Main(string[] args)
    {
    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);
    }
    }
      

  9.   


    很久以前,我也用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#的模式匹配写起来很优雅。赞一个
      

  10.   

    如果可直接用 data数组,就不用构造队列了,原理一样,代码看上去更省。 static void Main(string[] args)
    {
    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);
    }
    }
      

  11.   

    程序可以写清晰一点。(假设你学习了c#迭代器)可以写using System;
    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;
            }    }
    }
      

  12.   

    由于中间的 Tuple<int, int> 数据结构,并且需要start = end -1;之类的语句,仅使用一条 linq 查询确实是不容易写清晰算法的。c#完全写出成文自明的算法程序,并且很符合一般程序员的表达方式习惯(而不是F#那种方式)。
      

  13.   

    嗯,有个 + 号写成了 - 号了。呵呵我不认为 F# 语言有多大好处的!在 F# 刚出来的时候,我倒是感兴趣过一阵。结果发现它跟古老 Prolog 等相比都差得太远,在基本机制上缺乏古老的函数式工具所同时具有的一些人工智能程序自动回溯机制。http://bbs.csdn.net/topics/300080890我不会“放弃 VB.NET 之类的话”,因为它是 .net 平台很好的“编程体验”,对于c#程序员来说也是很好的补充。但是我觉得“可以坚决放弃 F# ”,因为它确实是多出来太多的累赘(我已经根本不相信 F# 有可能具有回溯机制机制了)。
      

  14.   

    抱歉没看各位大神的代码,我只是简单思考了思路既然是连续,那就是差值=1
    做一个结构,保存  a,a+1 的差值
    结果就会类似   2,2,-4,1,1,1,0,-9任何连续的1  就是你要的东西了至于语法,一直没兴起学习新东西,一直用简单的 for 好了各位谈论的语法,包括 Linq在内,是不是  在 原始实现阶段也是各种for?  我没细心看过,不对请喷我,我就知道了
      

  15.   

    噢噢还有这等热闹的贴,哈哈,不错.比起旁边游戏广告一个可爱女孩给老头一串冰糖葫芦更有吸引力.            int[] data = { 11, 12, 2, 4, 5, 6, 11, 12, 2, 4, 5, 6 };
                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() + ";";做个按思路简单的写法应该比较好理解
      

  16.   

    比如说为什么会考虑先决策要使用 Tuple<int,int> 数据结构,然后才写程序?为什么不是随便弄个
    差不多。这是编程语言课程上的题目。编程语言在软件专业来说,不算是任何一种很高级的基础理论课,只能算是一种职业技能。软件专业有几十门基础理论课,那些课程中的题目才算是“大学题目”。如果这这可题目有什么“大学题目”内容,让我来说,我觉得决策使用中间数据结构 Tuple<int,int> 是个体现了程序素质的考试点(首先考虑在选择数据结构上尽量规范和可扩展),这可以看出程序员在写一个程序时其实内心里边已经有了想为10倍的“将来应用”留余地的打算。
      

  17.   

    int[] rdn = { 0, 2, 5, 6, 7, 10, 12, 13, 15, 18 };            //这个问题和sql中的取连续数字的问题类似
                //上面数组中的每一项和它的索引相减会得到一个值
                //如果值是连续的,那么和索引相减得到的值是相同的,这个值成为分组因子
                //下面的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查询》的"数字辅助表,已有范围"章节
      

  18.   


    差不多。这是编程语言课程上的题目。编程语言在软件专业来说,不算是任何一种很高级的基础理论课,只能算是一种职业技能。软件专业有几十门基础理论课,那些课程中的题目才算是“大学题目”。如果这这可题目有什么“大学题目”内容,让我来说,我觉得决策使用中间数据结构 Tuple<int,int> 是个体现了程序素质的考试点(首先考虑在选择数据结构上尽量规范和可扩展),这可以看出程序员在写一个程序时其实内心里边已经有了想为10倍的“将来应用”留余地的打算。
    剖析得很专业哦~
      

  19.   

    Linq中的Aggregate即可完成,楼主可以了解下此方法特性