csansoon

Consolidation 3 P16072 Array intersection

Dec 28th, 2018
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<double> intersection(const vector<double>& v1, const vector<double>& v2) {
  6.     vector <double> C(0);
  7.     int i=0, j=0;
  8.     while (i<v1.size() and j<v2.size()) {
  9.         if (v1[i]==v2[j]) C.push_back(v1[i]);
  10.         if (v1[i]>v2[j]) do {++j;} while (v2[j] == v2[j-1]);
  11.         else do {++i;} while (v1[i] == v1[i-1]);
  12.     }
  13.     return C;
  14. }
  15.  
  16.  
  17. int main() {
  18.     int n;
  19.     cout<<"Vector 1:"<<endl;
  20.     cin>>n;
  21.     vector <double> V1(n);
  22.     for (int i=0; i<n; ++i) cin>>V1[i];
  23.     cout<<endl<<"Vector 2:"<<endl;
  24.     cin>>n;
  25.     vector <double> V2(n);
  26.     for (int i=0; i<n; ++i) cin>>V2[i];
  27.     vector <double> C = intersection(V1,V2);
  28.     cout<<endl<<endl<<"Intersection:"<<endl;
  29.     for (int i=0; i<C.size(); ++i) cout<<C[i]<<" ";
  30.     cout<<endl;
  31. }
  32.  
  33. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment