第一行两个整数,表示两个数组的长度, 接下来两行表示两个数字的值, 最后一行有四个整数a,b,c,d。 数组长度不会超过1000000。
每行一个整数,对应合并数组的下标在中间的那个值。
5 4 1 2 3 4 5 6 7 8 9 1 2 1 3
6
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int m = scanner.nextInt(); int n = scanner.nextInt(); int[] arr1 = new int[m]; int[] arr2 = new int[n]; for (int i = 0; i < arr1.length; i++) { arr1[i] = scanner.nextInt(); } for (int i = 0; i < arr2.length; i++) { arr2[i] = scanner.nextInt(); } int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int d = scanner.nextInt(); int[] arr = new int[b-a+1 + d-c+1]; for (int i = 0; i < arr.length; i++) { if (i < b-a+1) { arr[i] = arr1[a+i-1]; }else { arr[i] = arr2[c-1 + i - b+a-1]; } } if (arr.length % 2 == 0) { System.out.println(arr[arr.length/2-1]); }else { System.out.println(arr[arr.length/2]); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n1 = scanner.nextInt(); int n2 = scanner.nextInt(); int[] a1 = new int[n1]; for (int i = 0; i < n1; i++) a1[i] = scanner.nextInt(); int[] a2 = new int[n2]; for (int i = 0; i < n2; i++) a2[i] = scanner.nextInt(); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int d = scanner.nextInt(); int[] temp = new int[b - a + d - c + 2]; System.arraycopy(a1,a-1,temp,0,b-a+1); System.arraycopy(a2,c-1,temp,b-a+1,d-c+1); System.out.println(temp.length%2==0?temp[temp.length/2-1]:temp[temp.length/2]); } }