九度1004

/*********************************
 *    日期:2012-6-23
 *    作者:SJF0115
 *    题号: 九度1004
 *    题目:Median
 *    结果:AC
 *    题意:
 *    总结:
**********************************/
#include<stdio.h>
#include<string>
long int N,M;
long int sequence1[1000001];
long int sequence2[1000001];
int main()
{
    while(scanf("%ld",&N)!=EOF)
    {   
		int i,j;
		int medianNum,median,num = 0;
        for(i = 0;i<N;i++){
			scanf("%ld",&sequence1[i]);
		}
		scanf("%ld",&M);
		medianNum = ((N+M)%2==0)?(N+M)/2:(N+M)/2+1;
		for(i = 0;i<M;i++){
			scanf("%ld",&sequence2[i]);
		}
		for(i = 0,j = 0;i<N&&j<M;){
			num++;
			if(sequence1[i]<sequence2[j]){
				if(num == medianNum){
					median = sequence1[i];
					break;
				}
				i++;
			}
			else{
				if(num == medianNum){
					median = sequence2[j];
					break;
				}
				j++;
			}
		}
		if(num  == medianNum){
			printf("%d\n",median);
		}
		else{
			if(i < N){
				printf("%d\n",sequence1[i+medianNum - num - 1]);
			}
			else{
				printf("%d\n",sequence2[j+medianNum - num - 1]);
			}
		}
    }
    return 0;
}

你可能感兴趣的:(九度1004)