SDUTACM 顺序表应用5:有序顺序表归并

来源:互联网 发布:电脑翻墙好用的软件 编辑:程序博客网 时间:2024/05/22 17:37

Problem Description

已知顺序表A与B是两个有序的顺序表,其中存放的数据元素皆为普通整型,将A与B表归并为C表,要求C表包含了A、B表里所有元素,并且C表仍然保持有序。

Input

 输入分为三行:
第一行输入m、n(1<=m,n<=10000)的值,即为表A、B的元素个数;
第二行输入m个有序的整数,即为表A的每一个元素;
第三行输入n个有序的整数,即为表B的每一个元素;

Output

 输出为一行,即将表A、B合并为表C后,依次输出表C所存放的元素。

Example Input

5 31 3 5 6 92 4 10

Example Output

1 2 3 4 5 6 9 10

Hint

 
#include<stdio.h>#include<stdlib.h>struct hh{    int a[20010];    int n;};int main(){    struct hh *l,*l1,*l2;    l=(struct hh *)malloc(sizeof(struct hh));    l1=(struct hh *)malloc(sizeof(struct hh));    l2=(struct hh *)malloc(sizeof(struct hh));    int T,t,i,j,k,m;    scanf("%d",&l->n);    scanf("%d",&l1->n);    for(i=0;i<l->n;i++)        scanf("%d",&l->a[i]);    for(i=0;i<l1->n;i++)        scanf("%d",&l1->a[i]);    i=0;    j=0;    m=0;    while(i<l->n&&j<l1->n)    {        if(l->a[i]<l1->a[j])        {            l2->a[m]=l->a[i];            i++;            m++;        }        else        {            l2->a[m]=l1->a[j];            j++;            m++;        }    }    if(i<l->n)    {        for(i;i<l->n;i++)        {            l2->a[m]=l->a[i];            m++;        }    }    if(j<l1->n)    {        for(j;j<l1->n;j++)        {            l2->a[m]=l1->a[j];            m++;        }    }    for(i=0;i<l->n+l1->n-1;i++)        printf("%d ",l2->a[i]);    printf("%d\n",l2->a[i]);    return 0;}

0 0