高手们看看下面代码是否可以实现?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace TestFive
{
    class Program
    {
        static void Main(string[] args)
        {
            string sTest = "13,3,5,12,4,0,7,3,4,5,0,7,0,0,0,0,0,0,45,13,3,0,0,6,0,8,0";            List<int> listFive = new List<int>();            List<string> listSource = sTest.Split(',').ToList();            int n = listSource.Count - 4;            for (int j = 0; j < n; j++)
            {
                int nStartIndex = 0;                for (int i = 0; i < 5; i++)
                {
                    string sTemp = listSource[nStartIndex];                    listFive.Add(Int32.Parse(sTemp));                    nStartIndex++;
                }                if (IsLink(listFive))
                {
                    Console.WriteLine("Find");                    foreach (int i in listFive)
                    {
                        Console.WriteLine(i);
                    }                    Console.WriteLine();
                }                listSource.RemoveAt(0);                listFive = new List<int>();
            }            Console.ReadLine();
        }        static bool IsLink(List<int> list)
        {
            List<int> listTemp = new List<int>();            foreach (int nTemp in list)
            {
                listTemp.Add(nTemp);
            }            int nBegin = listTemp[0];            for (int i = 1; i < listTemp.Count; i++)
            {
                if (!(nBegin == 0 || listTemp[i] == 0 || (nBegin == listTemp[i] - i))) return false;
            }
            if (listTemp.Count == 1) return true;            listTemp.RemoveAt(0);            return IsLink(listTemp);
        }
    }
}

解决方案 »

  1.   

          static bool IsLink(List<int> list)
            {
                int temp = 0;
                for (int nI = 0; nI < list.Count; nI++)
                {
                    if (temp == 0)
                    {
                        if (list[nI] != 0)
                        {
                            temp = list[nI];
                            temp++;
                        }
                        continue;
                    }                if (temp == list[nI] || list[nI] == 0)
                    {
                        temp++;
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;        }