非递减有序集合合并

描述

巳知线性表LA和线性表LB中的数据元素按值非递减有序排列,现要求将LA和LB归并为一个新的线性表LC,且LC中的元素仍按值非递减有序排列。

输入三行,第一行A,B集合的个数n,m
第二行:集合A的数据;
第三行:集合B的数据。输出二行,第一行,集合C的个数k
第二行:集合C的数据。样例输入

11   12
2 4 6 7 8 9 12 34 56 78 89
3 5 7 9 12 34 56 98 234 456 789 1234

样例输出

18
2 3 4 5 6 7 8 9 12 34 56 78 89 98 234 456 789 1234

提示n,m<255

#include<bits/stdc++.h>
using namespace std;
struct Node
{
    int data;
    Node *next;
};
int main()
{
    set<int>a;
    int j=0,n,m,x;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
       scanf("%d",&x);
       a.insert(x);
    }
    for(int i=1;i<=m;i++)
    {
         scanf("%d",&x);
         a.insert(x);
    }
    printf("%d
",a.size());
    Node *head,*p,*s;
    head=new Node;head->next=NULL;
    p=head;


    for(set<int>::iterator it=a.begin();it!=a.end();it++)
    {
        s=new Node;
        s->data=*it;
        p->next=s;
        p=s;
    }
    p->next=NULL;
    p=head->next;
    while(p)
    {
        if(p->next==NULL)
        {
            printf("%d
",p->data);
        }
        else
        {
            printf("%d ",p->data);
        }
        p=p->next;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dean-SunPeishuai/p/10545695.html