#include<iostream>
using namespace std;
//将一个数插入到一个已经排好序的数组中
int main()
{
int a[11], n;
cout << "请输入数列:";
for (int i = 0; i < 10; i++)
cin >> a[i];
cout << endl << "请输入插入的数:";
cin >> n;
if (n > a[9])
a[10] = n;
else
for (int i = 0; i < 10; i++)
{
if (a[i] > n)
{
for (int j = 9; j >= i; j--) //最后一项往前直到a[i],均往后移动一位
a[j + 1] = a[j];
a[i] = n; //将n放在a[i]中
break;
}
}
cout << endl << "新数列:";
for (int i = 0; i < 11; i++)
cout << a[i] <<" ";
return 0;
}
来源:CSDN
作者:阿晶呐
链接:https://blog.csdn.net/weixin_44817229/article/details/104819219