zoukankan      html  css  js  c++  java
  • 非递减有序集合合并

    描述

    巳知线性表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;
    }
  • 相关阅读:
    对数可以用来简化乘法计算
    理解了一点github的用法了
    由摄氏温度和华氏温度转换想到的。
    CMD原来是支持通配符的啊
    怎么在CMD中创建文件
    如何学习数学
    SCILAB
    STS或eclipse安装SVN插件
    Html解析类的新选择CsQuery
    Tomcat编码问题
  • 原文地址:https://www.cnblogs.com/dean-SunPeishuai/p/10545695.html
Copyright © 2011-2022 走看看