fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. void readSet(vector<int>& v, istream_iterator<int>& input) {
  10. int size = *input++;
  11. while (size--) {
  12. v.push_back(*input++);
  13. }
  14. }
  15.  
  16. int main() {
  17. istream_iterator<int> input(cin);
  18. vector<int> a, b;
  19. readSet(a, input);
  20. readSet(b, input);
  21.  
  22. sort(a.begin(), a.end());
  23. sort(b.begin(), b.end());
  24.  
  25. vector<int> intersection;
  26. set_intersection(
  27. a.begin(), a.end(),
  28. b.begin(), b.end(),
  29. back_inserter(intersection)
  30. );
  31. intersection.erase(
  32. unique(intersection.begin(), intersection.end()), intersection.end()
  33. );
  34.  
  35. cout << "Ilosc powtarzajacych sie: " << intersection.size() << "\n";
  36. cout << "Liczby:\n";
  37. copy(
  38. intersection.begin(),
  39. intersection.end(),
  40. ostream_iterator<int>(cout, "\n")
  41. );
  42. return 0;
  43. }
Success #stdin #stdout 0s 3036KB
stdin
4

22 11 22 33 44 22

3

22 11 222 22 22
stdout
Ilosc powtarzajacych sie: 2
Liczby:
11
22