铂金钻石组第六题

数列有序!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 121165 Accepted Submission(s): 50002

Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output
对于每个测试实例,输出插入新的元素后的数列。

Sample Input
3 3
1 2 4
0 0

Sample Output
1 2 3 4
来自凌某人的分析:我觉得嘛…注意点空格和换行…也没什么好分析的吧?(怎么感觉除了最后一题到了后面几题越来越简单)
ac代码
#include
#include
using namespace std;
int main()
{
int n, m;
while (cin>>n>>m)
{
if (n == 0 && m == 0)
break;
int *_n = new int[n + 1];
for (int i = 0;i < n;i++)
{
cin >> _n[i];
}
int i = 0;
for (;i <= n;i++)
{
if (m < _n[i])
break;
}
for (int i1 = n;i1 != i;i1–)
{
_n[i1] = _n[i1 - 1];
}
_n[i] = m;
for ( i = 0;i < n ;i++)
{
cout << _n[i] << " ";
}
cout << _n[i] << endl;
delete _n;
}
}

猜你喜欢

转载自blog.csdn.net/weixin_44012186/article/details/84981526