同事問的問題,想了一下似乎這樣解就可以了...
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
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
全站熱搜
留言列表