【发布时间】:2021-11-11 16:40:26
【问题描述】:
我必须检查一个 int 数组是否按升序排序,例如 1、2、3、4 等。
所以这是我在伪代码中的尝试:
int[] arrayName = {1,2,3,4,5}; // This should be true
int[] arrayName2 = {5,4,3,6}; // This should be false
for (int i = 0; i < arrayName.Length; i++)
{
if (arrayName[i] < arrayName[i] - 1)
{
Console.WriteLine("The array is sorted");
}
else
Console.WriteLine("The array is not sorted");
}
我的问题:他们是一种检查当前迭代与前一个迭代的方法吗?我也不能在这个练习中使用任何库或扩展,所以基本上我只能使用“系统”
例如:
if (currentIteration > previousIteration)
Console.WriteLine("The array is sorted");
else
Console.WriteLine("The array is not sorted");
【问题讨论】: