Set Intersection
Time limit = 5 second(s)
Memory limit = 33000 Kb
Your task is to find the intersection of two sets of positive integers.
Input The input consists of two sets of positive integers A={a1, a2, ..., an} and B={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 < 106 and that the sets A and B are of size less than 1000.
Output is list of numbers or word "empty" if intersection is empty.
更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
注意地方:
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];//原来要防止多输出一个' ' } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索include
, oj
, of
, The
, that
sets
eikr 002、eikr002佐々木あき、intersection、python intersection、set intersection,以便于您获取更多的相关知识。