同事問的問題,想了一下似乎這樣解就可以了...

Code:
            int[] num = { 1, 2, 4, 5, 7, 11, 8, 9, 10 };
            for (int i = 0; i < num.Length; i++)
            {
                if (i == 0)
                {
                    if (num[i + 1] - num[i] != 1)
                    {
                        Console.WriteLine(i + ":" + num[i] + " is Err!");
                    }                     
                }
                else if (i == num.Length - 1)
                {
                    if ((num[i] - num[i-1] != 1))
                    {
                        Console.WriteLine(i + ":" + num[i] + " is Err!");
                    }
                }
                else
                {
                    if ((num[i + 1] - num[i] != 1) && (num[i] - num[i - 1] != 1))
                    {
                        Console.WriteLine(i + ":" + num[i] + " is Err!");
                    }                               
                }
            }
            Console.ReadKey();
        }

結果:
7、11

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 基因 的頭像
    基因

    老骨頭先生

    基因 發表在 痞客邦 留言(0) 人氣()