离散题目4

Problem Description

题目给出两个非空整数集,请写出程序求两个集合的交集。

Input

多组输入,每组输入包括两行,第一行为集合A的元素,第二行为集合B的元素。具体参考示例输入。 每个集合元素个数不大于3000,每个元素的绝对值不大于2^32 - 1。

Output

每组输入对应一行输出,为A、B的交集,如果交集为空输出"NULL",否则交集的元素按递增顺序输出,每个元素之间用空格分割。

Example Input
1 2 3 4 5
1 5 3 6 7
1 2 4 5 3
6 7 8 9 10
Example Output
1 3 5
NULL
code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int>a, b;
    vector<int>::iterator it;
    string buf, ac, bc;
    int t;
    while(getline(cin, ac))
    {
        getline(cin, bc);
        stringstream ss(ac);
        while(ss>>buf)
        {
            sscanf(buf.c_str(), "%d", &t);
            a.push_back(t);
        }
        stringstream cc(bc);
        while(cc>>buf)
        {
            sscanf(buf.c_str(), "%d", &t);
            b.push_back(t);
        }
        int i, flag = 0;
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        for(i = 0;i<a.size();i++)
        {
            it = find(b.begin(), b.end(), a[i]);
            if(it!=b.end())
            {
                if(flag) printf(" ");
                printf("%d", *it);
                b.erase(it);
                flag++;
            }
        }
        if(!flag) printf("NULL");
        printf("\n");
        a.clear();
        b.clear();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值