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, ..., a n} and b={b1, b2, ..., bk} Represented as two whitespace-separated lists of numbers. Each list ends With-1, which serves as an end-of-set marker. Repetitions of elements are possible in the input, but not in the output. May assume that 0 < ai, bi < ten6 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.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Note the place:
1 prevent repetition--use set here, the second time to enumerate the group, remember to erase set heavy data
2 output format This OJ is also very strict, not one more space
#include <iostream> #include <string> #include <vector> #include <algorithm> #includ
e <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];//original to prevent multiple output of one '}}