JosepRivaille

P12675: Elements comuns

Apr 29th, 2015
1,693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int elements_comuns (const vector<int>& X, const vector<int>& Y) {
  7.     int comuns = 0;
  8.     int x = X.size();
  9.     int y = Y.size();
  10.     int i = 0;
  11.     int j = 0;
  12.     while (i <= x-1 && j <= y-1) {
  13.         if (X[i] == Y[j]) {
  14.             ++comuns;
  15.             ++i;
  16.             ++j;
  17.         }
  18.         else if (X[i] > Y[j]) {
  19.             ++j;
  20.         }
  21.         else if (X[i] < Y[j]) {
  22.             ++i;       
  23.         }
  24.     }
  25.    
  26.     return comuns;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment