本帖最后由 topninaru0123 于 2009-07-10 11:07:39 编辑

解决方案 »

  1.   

    如果没有重复数字,那先排排序好了,然后再看有没有0,一个循环搞定,for(int i=0; i+4<arr.Length; i++)//没0的情况
    {
      if(arr[i+4]-arr[i]==4) return true;
    }
    for(int i=1; i+3<arr.Length; i++)//有0的情况,只需要判断4个数就可以了,第一位是0,跳过。
    {
      if(arr[i+3]-arr[i]==4) return true;
    }
      

  2.   

    2.我想学习算法,大侠们推存一下书籍...(语言关于C#,VB.net方面的),顺便学习算法的方法以及经验.
     就是算法设计和数据结构,,多练习
      

  3.   

    试试:        static void Main(string[] args)
            {
                Console.WriteLine(IsExist5InRow(new int[] { 1, 2, 8, 7, 4, 5, 3 }));
                Console.WriteLine(IsExist5InRow(new int[] { 4, 2, 9, 10, 6, 5, 3 }));
                Console.WriteLine(IsExist5InRow(new int[] { 1, 0, 8, 7, 4, 5, 3 }));
                Console.WriteLine(IsExist5InRow(new int[] { 7, 2, 9, 10, 6, 5, 3 }));
                Console.WriteLine(IsExist5InRow(new int[] { 0, 1, 9, 10, 6, 5, 3 }));
                Console.WriteLine(IsExist5InRow(new int[] { 0, 2, 9, 10, 8, 5, 3 }));
            }        static bool IsExist5InRow(int[] arr)
            {
                if (arr.Length < 5)
                    return false;
                Array.Sort(arr);
                int index = Array.LastIndexOf(arr, 0);
                int countOfZero = index + 1;
                bool exist = false;
                int counter = 0;
                for (int i = index + 2; i < arr.Length; i++)
                {
                    if (arr[i] == arr[i - 1] + 1)
                        counter++;
                    else if (arr[i] == arr[i - 1] + 2)
                    {
                        if (countOfZero > 0)
                        {
                            counter += 2;
                            countOfZero--;
                        }
                        else
                        {
                            counter = 0;
                            countOfZero = index + 1;
                        }
                    }
                    else
                    {
                        counter = 0;
                        countOfZero = index + 1;
                    }
                    if (counter == 4)
                    {
                        exist = true;
                        break;
                    }
                }
                return exist;
            }/*
    输出:
    True
    True
    True
    False
    False
    False*/
      

  4.   

    我之前写的取出连续几个数字的方法 你自己看看修改下long[] iNums ={ 13100000001, 13100000002, 13100000004, 13100000005, 13100000006, 13100000008, 13100000009, 13100000012, 13100000015, 13100000018, 13100000033 };
                List<long> liRes = GetNums(2, iNums);//取出连续两位的  private List<long> GetNums(int k, long[] iNums)
            {
                List<long> li = new List<long>();
                int j = 1;
                for (int i = 0; i < iNums.Length; i++)
                {
                    if (j == k)
                    {
                        if (i == iNums.Length - 1)
                        {
                            int ktemp = k;
                            while (ktemp > 0)
                            {
                                li.Add(iNums[i - ktemp + 1]);
                                ktemp--;
                            }
                        }
                        else if (iNums[i] + 1 != iNums[i + 1])
                        {
                            int ktemp = k;
                            while (ktemp > 0)
                            {
                                li.Add(iNums[i - ktemp + 1]);
                                ktemp--;
                            }
                            j = 1;
                        }
                        else
                        {
                            j++;
                        }
                    }
                    else if (j < k)
                    {
                        if (i == iNums.Length - 1)
                        {
                            //li.Add(iNums[i]);
                        }
                        else if (iNums[i] + 1 != iNums[i + 1])
                        {
                            //li.Add(iNums[i]);
                            j = 1;
                        }
                        else
                        {
                            j++;
                        }
                    }
                    else
                    {
                        if (i == iNums.Length - 1)
                        {
                            //li.Add(iNums[i]);
                        }
                        else if (iNums[i] + 1 != iNums[i + 1])
                        {
                            j = 1;
                        }
                        else
                        {
                            j++;
                        }
                    }
                }
                return li;
            }
      

  5.   

    大家太热情了..
    谢谢大家..
    第一题想要的只是思路而已,大概怎么做就可以了..问题已弄明白了..谢谢..第二题:
    除了2楼就说的<算法设计和数据结构>这本书籍之外,没有好的可推存的吗?
    或者专门讲解算法学习方面的网站什么的...
    本人对算法的知识很浅薄,希望从基本开始讲解的书籍以及网站. 
      

  6.   

    学习数据结构就好了
    http://download.csdn.net/source/493775
    这本C#版的数据结构去下载吧
      

  7.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApp4Test
    {
        public static class Question_001
        {
            public static int GetNumOfZero(int[] arrIn)
            {
                int NumOfZero = 0;            foreach (int i in arrIn)
                {
                    if (i == 0)
                    {
                        NumOfZero++;
                    }
                }            return NumOfZero;
            }        public static bool Contains(int iin, int[] arrIn)
            {
                foreach (int i in arrIn)
                {
                    if (i == iin)
                    {
                        return true;
                    }
                }            return false;
            }        public static bool HasFiveContinuous(int[] arrIn)
            {
                int ContinuousCount = 0;
                int OriginalZeroCount = GetNumOfZero(arrIn);
                int ZeroCount = OriginalZeroCount;            for (int i = 0; i <= 13; i++)
                {
                    if (Contains(i, arrIn))
                    {
                        ContinuousCount++;
                    }
                    else
                    {
                        if (ZeroCount != 0)
                        {
                            ZeroCount--;
                            ContinuousCount++;
                        }
                        else
                        {
                            ZeroCount = OriginalZeroCount;
                            ContinuousCount = 0;
                        }
                    }                if (ContinuousCount == 5)
                    {
                        return true;
                    }
                }            return false;
            }
        }    public static class Helper<T>
        {
            public static void WriteLineArr(T[] tIn)
            {
                StringBuilder strbMsg = new StringBuilder();            for (int i = 0; i < tIn.Length; i++)
                {
                    strbMsg.Append(tIn[i].ToString());
                    strbMsg.Append(" ");
                }            Console.WriteLine(strbMsg);
            }
        }
    }
      

  8.   

    写一个函数Fun(int i)处理没有零的情况的连续I个数是否存在
    然后查询数组中有几个零
    比如有两个零
    就调用Fun(3)
    没有零就调用Fun(5)至于Fun()函数的内容就简单了 由于不存在零 直接排序 然后判断连续就好了而且这样写可以扩展 别说是5个了 想改成50个都成