Advertisement
allia

динамическое слияние 2 сортированных массивов

Sep 22nd, 2020
1,456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.   int n=0, m=0;
  8.   cin >> n >> m;
  9.   int *arr, *ar, *res;
  10.  
  11.   arr = new int[n];
  12.   ar = new int[m];
  13.   res = new int[n+m];
  14.  
  15.   for (int i=0; i<n; i++)
  16.   cin >> arr[i];
  17.  
  18.   for (int j=0; j<m; j++)
  19.   cin >> ar[j];
  20.  
  21.    int i=0, j=0, c=0;
  22.  
  23.   while ( i<n && j<m)
  24.   {
  25.     if (arr[i]>ar[j])
  26.       res[c++]=ar[j++];
  27.     else
  28.       res[c++]=arr[i++];
  29.   }
  30.   while (i<n)
  31.   res[c++]=arr[i++];
  32.   while (j<m)
  33.   res[c++]=ar[j++];
  34.  
  35.   for (int l=0; l<n+m; l++)
  36.   cout << res[l] << " ";
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement