HDoj 2019 数列有序!

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
 

 

Author
lcy
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2021 2020 2022 2023 2024 
 
 
C语言代码如下:
#include<stdio.h> 
#include<malloc.h>
int main()
{
    int n,m;
    while( scanf("%d%d",&n,&m)!=EOF )
    {
        if(n==0&&m==0)
            break;
        int *a=(int *)malloc(sizeof(int)*(n+1));
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
        {
            if(m<a[i])
            {
                for(int j=n-1;j>=i;j--)
                    a[j+1]=a[j];
                a[i]=m;    
                break;        
            }            
        }
        printf("%d",a[0]);            //注意每行末尾不要多出一个空格 
        for(int i=1;i<n+1;i++)
            printf(" %d",a[i]);
        printf("\n"); 
    }
    return 0;
}

 

 
posted on 2020-03-19 19:39  沈香茶  阅读(161)  评论(0)    收藏  举报