只判断小于的情况代码要更简洁xie
//从小到大的数组插入一个数排序
string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
int number = Convert.ToInt32(strArray[i]);
intArray[i] = number;
}
int x = Convert.ToInt32(Console.ReadLine());
int xIndex = 0;
for (int i = 0; i < intArray.Length; i++)
{
if (intArray[i] >= x)
{
xIndex = i;
break;
}
else
{
xIndex = intArray.Length;
}
}
int[] newIntArray = new int[intArray.Length + 1];
for (int i = 0; i < newIntArray.Length; i++)
{
if (i < xIndex)
{
newIntArray[i] = intArray[i];
}
else if (i == xIndex)
{
newIntArray[i] = x;
}
else
{
newIntArray[i] = intArray[i - 1]; //1 3 4 5
}
}
foreach (int t in newIntArray)
{
Console.Write(t + " ");
}
