Advertisement
CosminVarlan

reuniune_intersectie

Mar 19th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int n,m,a[100000],b[100000],ela,elb;
  9. int contor=0;
  10.  
  11.  
  12.  
  13.  
  14. int main()
  15. {
  16.     cin >> n;
  17.     for(int i=0; i<n; i++)
  18.     {
  19.         cin >> a[i];
  20.     }
  21.     cin >> m;
  22.     for(int i=0; i<m; i++)
  23.     {
  24.         cin >> b[i];
  25.     }
  26.  
  27.  
  28.     int p1 = 0;
  29.     int p2 = 0;
  30.  
  31.     while(p1 < n && p2 < m)
  32.     {
  33.         if (a[p1] < b[p2])
  34.         {
  35.             cout << a[p1] << ' ';
  36.             p1++;
  37.             continue;
  38.         }
  39.         if (a[p1] > b[p2])
  40.         {
  41.             cout << b[p2] << ' ';
  42.             p2++;
  43.             continue;
  44.         }
  45.         if (a[p1] == b[p2])
  46.         {
  47.             cout << b[p2] << ' ';
  48.             p1++;
  49.             p2++;
  50.         }
  51.     }
  52.     while(p1<n)
  53.     {
  54.         cout << a[p1] << ' ';
  55.         p1++;
  56.     }
  57.     while(p2<m)
  58.     {
  59.         cout << b[p2] << ' ';
  60.         p2++;
  61.     }
  62.  
  63.  
  64.  
  65.  
  66.     cout <<  '\n';
  67.  
  68.  
  69.  
  70.  
  71.     p1 = 0;
  72.     p2 = 0;
  73.  
  74.     while(p1 < n && p2 < m)
  75.     {
  76.         while(a[p1] != b[p2] && (p1 < n && p2 < m))
  77.         {
  78.             while (a[p1] < b[p2]) p1++;
  79.             while (a[p1] > b[p2]) p2++;
  80.         }
  81.  
  82.         if (a[p1] == b[p2])
  83.         {
  84.             cout << b[p2] << ' ';
  85.             p1++;
  86.             p2++;
  87.         }
  88.     }
  89.  
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement