我正在尝试编写一个方法(
因此,对于
请问有人能帮我找到这里的错误吗?
public bool Seq_Check(int[] A, int k)
),用于检查数组A
是否包含数字1,2,...,k
(每个数字至少出现一次),且不包含其他数字。因此,对于
myArr1
和myArr2
,它应该分别返回true
和false
,这也是正确的。但错误地显示第三个数组为true
。请问有人能帮我找到这里的错误吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayChecker
{
class Program
{
/// The program's main entry point.
static void Main(string[] args)
{
// Input arrays are assumed to be in non-decreasing order.
int[] myArr1 = new int[] { 1, 1, 2, 3, 3 };
int[] myArr2 = new int[] { 1, 1, 3 };
int[] myArr3 = new int[] { 1, 1, 1, 2, 3, 4, 5, 6, 1 };
// Printing to console True & False respectively.
Console.WriteLine( Seq_Check(myArr1, 3) );
Console.WriteLine( Seq_Check(myArr2, 2) );
Console.WriteLine( Seq_Check(myArr3, 6) );
// Prevent the console window from closing.
Console.ReadLine();
}
/// This method checks whether array A contains numbers 1,2,...,k
/// (every number from 1 to k exactly once) and no other numbers.
public static bool Seq_Check(int[] A, int k)
{
int n = A.Length;
for (int i = 0; i < n-1; i++)
{
if ( A[i]+1 < A[i+1] )
return false;
}
if ( A[0] != 1 && A[n-1] != k )
return false;
else
return true;
}
}
}
Seq_check
来查看哪一个return false
语句正在执行吗? - ChrisFk
是6,所以应该是1, 2, 3, 4, 5, 6
,但实际上是1,2,3,3,4,5,6
(根据我的理解)。 - keyboardP