HDU1412-{A} + {B}

解决两个整数集合的合并问题,确保输出结果中没有重复元素,并按升序排列。提供两种实现方式,一种使用STL set,另一种使用数组进行排序和去重。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

{A} + {B}

                                                                         Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                                    Total Submission(s): 19882    Accepted Submission(s): 8258


Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.
 

Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.
 

Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.
 

Sample Input
  
1 2 1 2 3 1 2 1 1 2
 

Sample Output
  
1 2 3 1 2
 

Author
xhd
 

Source
 
解题思路:可以用STL,也可以用数组


STL:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

int main()
{
    int a,n1,n2;
    while(~scanf("%d %d",&n1,&n2))
    {
        set<int>s;
        for(int i=0;i<n1+n2;i++)
        {
            scanf("%d",&a);
            s.insert(a);
        }
        set<int>::const_iterator it;
        it=s.begin();
        while(it!=s.end())
        {
            printf("%d",*it);
            it++;
            if(it!=s.end())
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}


数组:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <climits>

using namespace std;

int main()
{
    int a[20002],b[20002],m,n;
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=0;i<m+n;i++)
            scanf("%d",&a[i]);
        sort(a,a+m+n);
        b[0]=a[0];
        int j=0;
        for(int i=1;i<m+n;i++)
        {
            if(b[j]!=a[i])
            {
                j++;b[j]=a[i];
            }
        }
        for(int i=0;i<j;i++)
            printf("%d ",b[i]);
        printf("%d\n",b[j]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值