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;
}
2832

被折叠的 条评论
为什么被折叠?



