Set Intersection
Time limit = 5 second(s)
Memory limit = 33000 Kb
Your task is to find the intersection of two sets of positive integers.
InputThe input consists of two sets of positive integersA={a1,a2, ...,an} andB={b1,b2, ...,bk}
represented as two whitespace-separated lists of numbers. Each list ends with -1, that serves as an end-of-set marker. Repetitions of elements are possible in the input, but not in the output. You may assume that 0 <ai,bi<106and
that the setsAandBare of size less than 1000.
Outputis list of numbers or word "empty" if intersection is empty.
Input#1
6 7 8 1 2 3 -1
4 3 2 1 1 -1
|
Output#1
1 2 3
|
Input#2
1 2 3 -1
4 4 5 5 6 6 -1
|
Output#2
empty
|
注意地方:
1 防止重复 -- 这里使用set,第二次遍历数组的时候,记得把set重的数据erase掉
2 输出格式这个oj也很严格的,不能多一个空格
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int A[1000] = {0};
void SetIntersection()
{
set<int> si;
int a = 0;
while (cin>>a && -1 != a)
{
si.insert(a);
}
int j = 0;
while (cin>>a && -1 != a)
{
if (si.count(a))
{
A[j++] = a;
si.erase(a);
}
}
if (!j) cout<<"empty";
else
{
j--;
for (int i = 0; i < j; i++)
{
cout<<A[i]<<' ';
}
cout<<A[j];//原来要防止多输出一个' '
}
}
分享到:
相关推荐
Judge.m蚁群算法
ComplexSet复数集合算法的C++实现,还包括一些参考资料:贪心算法、排课程序、选课程序。... (10)Judge if Set is empty or full (11)Find if the given element exist in the Set Author : Wang Ting
ComplexSet复数集合算法的C 实现,还包括一些参考资料:贪心算法、排课程序、选课程序。...(10)Judge if Set is empty or full (11)Find if the given element exist in the Set Author : Wang Ting
百柱在线裁判用python解决算法问题
ACM_online_judge例题讲解, 有详细的例题分析和算法分析与计算.
高级算法-nju-onlineJudge-master-java和python的答案+考试题目
Judge
judge uml绘图软件 小巧
nju-onlineJudge:南京大学高级算法课OJ
青岛大学开源 Online Judge | QQ群 496710125 。。。。。
Filename : ComplexSet.cpp Intro : This is a Class of Set whoes ...(10)Judge if Set is empty or full (11)Find if the given element exist in the Set Author : Wang Ting Date : 2009-11 Language : C++
浙江大学acm online judge 浙江大学acm online judge 浙江大学acm online judge
基于PHP,实现online judge 设计.
├──Package │ ├── Sort 排序篇 ...│ ├── Kmp....│ └── Judge.php 面试题之扑克牌中任选五张判断是不是顺子 │ └── Factorial.php 面试题之N的阶乘末尾有多少个0
3.AI-Judge.rar
希冀平台onlineJudge使用说明
四川大学onlinejudge内核 c++代码 自己看看吧。。。
项目使用技术:前端(bootstrap+layui+jquery)后端(SpringMVC+Mysql+Mybatis),客户端支持在线做题,有在线编译器,支持c,c++,java三种语言,可写题解发布论坛、公告,用户可评论回复;服务端使用layui后台管理...
Online Judge Problem Code
OnlineJudge站点网址.txtOnlineJudge站点网址.txt