Advertisement
vov44k

Слияние 2х массивов

Feb 27th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.  
  8.         int n = in.nextInt();
  9.         int m = in.nextInt();
  10.         int[] a = new int[n];
  11.         int[] b = new int[m];
  12.         for (int i = 0; i < n; i++) {
  13.             a[i] = in.nextInt();
  14.         }
  15.         for (int i = 0; i < m; i++) {
  16.             b[i] = in.nextInt();
  17.         }
  18.  
  19.         int[] c = new int[n+m];
  20.         int i = 0;
  21.         int j = 0;
  22.         int k = 0;
  23.         while (i < n && j < m){
  24.             if (a[i] < b[j]){
  25.                 c[k++] = a[i++];
  26.             } else {
  27.                 c[k++] = b[j++];
  28.             }
  29.         }
  30.  
  31.         while (i < n ){
  32.             c[k++] = a[i++];
  33.         }
  34.  
  35.         while (j < m){
  36.             c[k++] = b[j++];
  37.         }
  38.  
  39.         for (int t = 0; t < n + m; t++) {
  40.             System.out.print(c[t] + " ");
  41.         }
  42.         in.close();
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement