hduoj2019数列有序

有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

# include
# include
struct node
{
    int data;
    struct node *next;
};
int main()
{   struct node *head,*p,*q,*t;
    int m,n,i,a;
    while((scanf("%d%d",&n,&m)!=EOF)&&(m!=0&&n!=0))
    {
        head=NULL;
        for(i=1;i<=n;i++)
        {   scanf("%d",&a);
            p=(struct node *)malloc(sizeof(struct node));
            p->data=a;
            p->next=NULL;
            if(head==NULL)
                head=p;
            else
                q->next=p;
            q=p;
        }
        t=head;
        while(t!=NULL)
        {
            if(t->next==NULL||t->next->data>m)
            {
                p=(struct node *)malloc(sizeof(struct node));
                p->data=m;
                p->next=t->next;
                t->next=p;
                break;
            }
            t=t->next;
        }
        t=head;
        while(t!=NULL)
        {   if(t->next!=NULL)
            printf("%d ",t->data);
            else
                printf("%d",t->data);
            t=t->next;
        }
        printf("\n");
    }
return 0;
}

hduoj2019数列有序_第1张图片
格式错误两次,修改了输出的空格和换行就可以ac了

你可能感兴趣的:(hduoj2019数列有序)