非递减有序集合合并

描述

巳知线性表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\n",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\n",p->data);
        }
        else
        {
            printf("%d ",p->data);
        }
        p=p->next;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/dean-SunPeishuai/p/10545695.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值