List<int> list1 = new List<int>(){1,2,5,12,10,4,5};List<int> list2 = new List<int>(){0,1,4,2,7,8,9};
功能要求:
查询list1、list2,要求其元素存在连续关系的个数至少是3个,或以上,求出list1、list2哪个集合满足条件。上面的只有list2满足条件,其7、8、9是连续的,并且个数是3个,而list1的连续个数1、2或4、5只有2个连续的,所以,不满足条件。
给出查询代码,谢谢!!

解决方案 »

  1.   

    调用 List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };                List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };                bool flag1 = CheckList(list1,3);//false
                    bool flag2 = CheckList(list2, 3);//true
    public bool CheckList(List<int> list,int count)
                {
                    bool flag = false;
                    var result = new List<List<int>>();//存贮连续片选集合
                    var query = list.Aggregate((i, j) =>
                    {
                        if (j == i + 1)
                        {
                            if (result.Count() > 0 && result.Last().Last() + 1 == j)
                            {
                                result.Last().Add(j);
                            }
                            else 
                            { 
                                result.Add(new List<int> { i, j });
                            };
                        }
                        return j;
                    }
                    );
                    flag = result.Max(a=>a.Count())>=count;
                    return flag;
                }
      

  2.   

    请问CheckList是什么东西,MSDN我没查到
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace wanlinq
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };            List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };            Console.WriteLine(panduan(list1));            Console.WriteLine(panduan(list2));            Console.ReadLine();
            }        static bool panduan(List<int> myinput)
            {
                List<List<int>> re = new List<List<int>>();
                //默认list里面至少有3个元素
                for (int i = 0; i < myinput.Count - 2; i++)
                {
                    List<int> re1 = new List<int>();
                    re1.Add(myinput[i]);
                    re1.Add(myinput[i + 1]);
                    re1.Add(myinput[i + 2]);
                    re.Add(re1);
                }            return re.Any(x =>
                {
                    if (x[0] + 1 == x[1] && x[0] + 2 == x[2])
                    {
                        return true;
                    }
                    return false;
                }
                    );
            }
        }
    }
      

  4.   

     static    bool getRes (IEnumerable<int> list)
              
         {
                
               if (list==null)
                {
                    return false;
                
                }        return   Enumerable.Range(1, list.Count() - 2).Any(index => {
                int first = list.ElementAt(index - 1);
                int mid = list.ElementAt(index);
                int end = list.ElementAt(index + 1);            return first < mid && mid < end && (mid * 2 == first + end);
                
                });           }测试代码  List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };
                 List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };            bool res1 = getRes(list1); //结果false
                bool res2 = getRes(list2); //结果true
      

  5.   

    List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };            List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };
                Console.WriteLine(Enumerable.Range(0, list2.Count() - 2).Any(index => list2[index] + list2[index + 2] == 2*list2[index + 1]));
                Console.Read();
      

  6.   


            public static  bool ListCheck(System.Collections.Generic.List<int> list)
            {
                return list.Where((t, index) => index > 0 && index < list.Count() - 1 && t * 2 == list[index - 1] + list[index + 1]).Count() > 0;
            }
      

  7.   


    public bool ListCheck(System.Collections.Generic.List<int> list)
       {
       return list.Where((t, index) => index > 0 && index < list.Count() - 1&&list[index-1]<list[index]&&list[index]<list[index+1] && t * 2 == list[index - 1] + list[index + 1]).Count() > 0;
       }