kruchininkonstantin

полурешена

Oct 15th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Main {
  4.  
  5.     public static int[] merge (int[] a, int[] b) {
  6.         int[] result = new int[a.length + b.length];
  7.         int ia = 0, ib = 0;
  8.             for (int i = 0; i < result.length; i++) {
  9.                 if (ia > a.length-1) {
  10.                     result[i] = b[ib];
  11.                     ib++;
  12.                 }
  13.                 else if (ib > b.length-1){ //списал. понимаю, как работает, но думаю, что можно короче(
  14.                     result[i] = a[ia];
  15.                     ia++;
  16.                 }
  17.                 else if (a[ia] < b[ib]){
  18.                     result[i] = a[ia];
  19.                     ia++;
  20.                 }
  21.                 else{
  22.                     result[i] = b[ib];
  23.                     ib++;
  24.                 }
  25.             }
  26.         /*
  27.         for (int i = 0; i < a.length; i++) {
  28.             for (int j = 0; j < b.length; j++) {
  29.                 if (a[i] < b[j]) {
  30.                     result[i + j] = a[i];
  31.                     System.out.println(Arrays.toString(result)); //знаю, почему это не работает, но не знаю, как исправить(
  32.                 } else {
  33.                     result[i + j] = b[j];
  34.                     System.out.println(Arrays.toString(result));
  35.                 }
  36.             }
  37.         }
  38.         */
  39.         return result;
  40.     }
  41.  
  42.     public static void main(String[] args) {
  43.         int[] x = new int[] {0, 1, 5};
  44.         int[] y = new int[] {2, 3, 6};
  45.         int[] z = merge(x, y);
  46.  
  47.         System.out.println(Arrays.toString(z));
  48.     }
  49. }
Add Comment
Please, Sign In to add comment