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

顺序表应用5:有序顺序表归并
Time Limit: 100 ms Memory Limit: 880 KiB
Submit Statistic
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所存放的元素。

Sample Input
5 3
1 3 5 6 9
2 4 10
Sample Output
1 2 3 4 5 6 9 10

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define max 10001
using namespace std;

typedef int ElemType;

typedef struct
{
    ElemType *elem;
    int length;
    int listsize;
}SqList;

void InitSqList(SqList &L)
{
    L.elem=(ElemType*)malloc(max* sizeof(ElemType));
    L.length = 0;
    L.listsize = max;
}
void List_insert(SqList &L, int len)
{
    int i;
    for(i=0; i<len; i++)
    {
        cin>>L.elem[i];
    }
    L.length = len;
}
void List_h(SqList &L1, SqList &L2, SqList &L3)
{
        int i, j, k;
        k=0;
        i=j=0;
        while(i<L1.length&&j<L2.length)
        {
               if(L1.elem[i]<L2.elem[j])
                {
                    L3.elem[k]=L1.elem[i];
                    k++;
                    i++;
                }
                else
                {
                    L3.elem[k]=L2.elem[j];
                    k++;
                    j++;
                }
        }
             while(j<L2.length)
             {
                 L3.elem[k]=L2.elem[j];
                 k++;
                 j++;
             }
             while(i<L1.length)
             {
                 L3.elem[k]=L1.elem[i];
                 k++;
                 i++;
             }
             for(i=0; i<L1.length+L2.length-1; ++i)
                printf("%d ", L3.elem[i]);
             printf("%d\n", L3.elem[i]);
}
int main()
{
    SqList L3;
    InitSqList(L3);
    SqList L1, L2;
    InitSqList(L1);
    InitSqList(L2);
    int m, n;
    cin>>m>>n;
    List_insert(L1, m);//添加元素
    List_insert(L2, n);
    List_h(L1, L2, L3);
    return 0;
}


/***************************************************
User name: jk160532姜兴友
Result: Wrong Answer
Take time: 16ms
Take Memory: 308KB
Submit time: 2017-09-21 15:12:55
****************************************************/


/***************************************************
User name: jk160532姜兴友
Result: Accepted
Take time: 16ms
Take Memory: 324KB
Submit time: 2017-09-21 16:21:58
****************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值