uzimane_

слияние двух отсортированных массивов

Jul 10th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n1, n2, i;
  7.  
  8. freopen("input.txt", "r", stdin);
  9. freopen("output.txt", "w", stdout);
  10.  
  11. cin >> n1 >> n2;
  12. int* a = new int[n1], * b = new int[n2], * c = new int[n1 + n2];
  13.  
  14. for (i = 0; i < n1; i++)
  15. {
  16. cin >> a[i];
  17. }
  18.  
  19. for (i = 0; i < n2; i++)
  20. {
  21. cin >> b[i];
  22. }
  23.  
  24. int j = 0, k = 0, h = 0;
  25.  
  26. while (j < n1 && k < n2)
  27. {
  28. if (a[j] <= b[k]) c[h++] = a[j++];
  29. else c[h++] = b[k++];
  30. }
  31.  
  32. while (j < n1) c[h++] = a[j++];
  33.  
  34. while (k < n2) c[h++] = b[k++];
  35.  
  36. for (i = 0; i < n1 + n2; i++)
  37. {
  38. cout << c[i] << " ";
  39. }
  40. }
Add Comment
Please, Sign In to add comment