private bool Test(List<long> phongs) { 
            ....
        }
求个算法,phones是一个手机号集合,估计一次传进来有2000个手机号,判断是否有10连号,比如说15117172368,15117172369,15117172370 就是一个3连号,若有返回false,没有则返回true

解决方案 »

  1.   

    本帖最后由 caozhy 于 2011-08-05 13:54:26 编辑
      

  2.   


    ……
    int j =0;
        for (int i = 2; i < data.Count; i++)
        {
            if (data[i] == data[i - 1] + 1)
    {
    j++;
    }
    else
    {
    j=0;
    }
    if(j == 9)
    {
    return ture;
    }
        }
    return false;
    ……
    新手,有误请指教。
      

  3.   

    private bool Test(List<long> phongs)
    {
        List<long> data = phongs.OrderBy(x => x).ToList();
        for (int i = 0; i < data.Count()-10; i++)
        {
            if(data[i+9]-data[i]==10)
               return true;
        }
        return true;
    }
      

  4.   

    LS好方法。。
    就是最后false写成true了……
      

  5.   

    IF 错了,应该是data[i+9] - data[i]==9, 似乎是,随手写的,没调试
      

  6.   


    CS1061: “System.Collections.Generic.List<long>”不包含“OrderBy”的定义,并且找不到可接受类型为“System.Collections.Generic.List<long>”的第一个参数的扩展方法“OrderBy”(是否缺少 using 指令或程序集引用?)
    有报错?
      

  7.   

    那就掉用Sort方法, ORDERBY是3.5的扩展方法
      

  8.   

    你如果LIST已经是排好序的话就不用ORDERBY了。
      

  9.   

            private bool Test(List<long> phongs)
            {
                phongs.Sort();
                for (int i = 0; i < phongs.Count - 10; i++)
                {
                    if (phongs[i + 9] - phongs[i] == 9)
                    {
                        return true;
                    }
                }
                return false;
            }
      

  10.   


    private bool Test(List<long> phongs,int n) { 
                //排序,可以用sortfor (int i = 2; i < phongs.Count(); i=i+n/2)
        {
            for(j=1;j<n;j++)
                {
                     判定
                 }
        }
            }
      

  11.   

            static void Main(string[] args)
            {
                //我就没有写排序的了,需要先排序(自增),然后 运行下面
                List<int> phones = new List<int>();
                for (int i = 0; i < 20; i++)
                {
                    phones.Add(1377 + i);  //保证自增
                }            phones.Remove(1377);
                phones.Remove(1388);            List<int> result = new List<int>();            for (int i = phones.Count-1; i >0; i--)
                {                if (phones[i] == phones[i - 1]+1)
                    {
                        result.Add(phones[i]);
                    }
                    else if(phones[i] != phones[i - 1]+1&&result.Count < 9)
                    {
                        result.Clear();
                    }
                }
                if (result.Count == 9)
                {
                    result.Add(phones[0]);
                    Console.Write("10  Continuous");  //对应于 return  ture
                    Console.ReadLine();
                }
            }
      

  12.   

    本帖最后由 caozhy 于 2011-08-09 15:00:30 编辑