Advertisement
Dani_info

interclasarea a doi vectori

Mar 27th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {  
  7.     char caz;
  8.     cout << "Introdu cazul:"; cin >> caz;
  9.     switch (caz)
  10.     {
  11.         case 'i':
  12.         {
  13.             int n, m, a[100], b[100], c[200];
  14.             cout << "n="; cin >> n;
  15.             cout << "m="; cin >> m;
  16.             for (int i = 0; i < n; ++i)
  17.                 cin >> a[i];
  18.             cout << "===============";
  19.             cout << endl;
  20.             for (int i = 0; i < m; i++)
  21.                 cin >> b[i];
  22.             int i = 0, j = 0, k = 0;
  23.             while (i < n && j < m)
  24.             {
  25.                 if (a[i] > b[j])
  26.                 {
  27.                     c[k] = b[j];
  28.                     j++;
  29.                     k++;
  30.                 }
  31.                 else
  32.                 {
  33.                     c[k] = a[i];
  34.                     i++;
  35.                     k++;
  36.                 }
  37.             }
  38.             if (i <= n)
  39.             {
  40.                 for (int x = i; x < n; x++)
  41.                 {
  42.                     c[k] = a[x];
  43.                     k++;
  44.                 }
  45.             }
  46.             if (j <= m)
  47.             {
  48.                 for (int y = j; y < m; y++)
  49.                 {
  50.                     c[k] = b[y];
  51.                     k++;
  52.                 }
  53.             }
  54.             for (int i = 0; i <k; i++)
  55.                 cout << c[i] << " ";
  56.             break;
  57.         }
  58.     }
  59.     system("pause");
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement