![](http://ovteww5gs.bkt.clouddn.com/17-9-21/21
531353.jpg)
package xiecheng;
import java.util.Scanner;
public class midNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();// 第一个数组的元素个数
int[] arr1 = new int[n];
for (int i = 0; i < n; ++i) {
arr1[i] = sc.nextInt();
}
int m = sc.nextInt();// 第二个数组的元素个数
int[] arr2 = new int[m];
for (int i = 0; i < m; ++i) {
arr2[i] = sc.nextInt();
}
double res = middleNumberOfTwoArr(arr1, arr2);
System.out.println(res);
}
private static int middleNumberOfTwoArr(int[] arr1, int[] arr2) {
int start1 = 0;
int start2 = 0;
int end1 = arr1.length - 1;
int end2 = arr2.length - 1;
int midIndex1;
int midIndex2;
while (start1 < end1 || start2 < end2) {
midIndex1 = (start1 + end1) / 2;
midIndex2 = (start2 + end2) / 2;
if (arr1[midIndex1] == arr2[midIndex2]) {
return arr1[midIndex1];
}
if (arr1[midIndex1] < arr2[midIndex2]) {
if (((end1 - start1) % 2 == 0) && ((end2 - start2) % 2 == 0)) {
start1 = midIndex1;
end2 = midIndex2;
} else {
start1 = midIndex1 + 1;
end2 = midIndex2;
}
} else {
if (((end1 - start1) % 2 == 0) && ((end2 - start2) % 2 == 0)) {
start2 = midIndex2;
end1 = midIndex1;
} else {
start2 = midIndex2 + 1;
end1 = midIndex1;
}
}
}
if (arr1[start1] < arr2[start2]) {
return arr1[start1];
}else if (arr1[start1] > arr2[start2]) {
return arr2[start2];
}else {
return (arr1[start1]+arr2[start2])/2;
}
}
}
提交结果;
14%
参考:
http://blog.csdn.net/huberjobs/article/details/51102970