155-练习8 循环练习
8,有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。输出新的序列
int[] numArray = { 2, 4, 4, 6, 67, 785, 3244 };
int num = Convert.ToInt32(Console.ReadLine());
int[] numNew = new int[numArray.Length + 1];
int index = 0;
bool isInsert = false;
for (int i = 0; i < numNew.Length; i++)
{
if (i == numArray.Length && isInsert == false)
{
numNew[i] = num;
isInsert = true;
break;
}
if (num <= numArray[index] && isInsert == false)
{
numNew[i] = num;
isInsert = true;
}
else
{
numNew[i] = numArray[index];
index++;
}
}
for (int i = 0; i < numNew.Length; i++)
{
Console.Write(numNew[i] + " ");
}
Console.ReadKey();
浙公网安备 33010602011771号