PAT:1029. Median (25) AC
#include<stdio.h>
#include<algorithm>
using namespace std;
int arr1[1000066];
int arr2[1000066];
int main()
{
int n1,n2;
scanf("%d",&n1);
for(int i=0 ; i<n1 ; ++i)
{
scanf("%d",&arr1[i]);
}
scanf("%d",&n2);
for(int i=0 ; i<n2 ; ++i)
{
scanf("%d",&arr2[i]);
}
arr1[n1]=0x7fffffff,arr2[n2]=0x7fffffff; //【caution】因为输出的时候是比较最小数的,不妨设数组最后一个数的结尾是无穷大
int middle=(n1+n2-1)/2;
int i1=0,i2=0; //代表两个数组当前指向,从0开始
for(int i=0 ; i<middle ; ++i) //找到第middle小的数即可
{
if(arr1[i1]<arr2[i2]) //谁小谁往前
++i1;
else
++i2;
}
if(arr1[i1]<arr2[i2]) //已剔除前middle个最小数,这里最小的一个就是中间值了
printf("%d",arr1[i1]);
else
printf("%d",arr2[i2]);
return 0;
}