077484@TOC
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
code
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
// An highlighted block
#include <stdio.h>
int main()
{
int m,n,a,c,h,t,i,j;
while (scanf("%d%d",&n,&m)!=EOF)
{
int a[n+1];
if(n==0 && m==0)
break;
else
for(c=0;c<n;c++)//赋初值
{
scanf("%d",&a[c]);
}
a[c]=m;
for(i=0;i<n;i++)
{
if(a[i]<=m&&a[i+1]>=m)//查找
{
for(j=n;j>i;j--)
{
a[j]=a[j-1];//移动,腾出空位
}
a[j+1]=m;//赋值
break;
}
}
printf("%d",a[0]);
for(h=1;h<n+1;h++)
{
printf(" %d",a[h]);
}
printf("\n");
}
return 0;
}