九度oj-1097

题目描述:

存在两组数组,和4个数字a,b,c,d,要求做如下操作,将第一个数组第a个数到第b个数,第二个数组的第c个数到第d个数放到一个数组中,求出合并后数组的中间值,如果有两个中间值,取下标较小的那个。

输入:

第一行一个整数t表示有t个测试数据
第二行两个整数,表示两个数组的长度,
接下来两行表示两个数字的值,

最后一行有四个整数a,b,c,d。

数组长度不会超过1000000。

输出:

每行一个整数,对应合并数组的下标在中间的那个值。

样例输入:

1
5 4
1 2 3 4 5
6 7 8 9
1 2
1 3

样例输出:

6

代码:

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

int arrayA[1000001];  
int arrayB[1000001];  
int arrayC[2000002];  

int main()  
{  
    int n,i,j,m,index,lena,lenb;  
    int a,b,c,d;  
    //freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin);   
    while(scanf("%d",&n)!=EOF)  
    {  
        for(i = 0;i < n;i++){  
            //数组长度  
            scanf("%d %d",&lena,&lenb);  
            //第一个数组  
            for(j = 0;j < lena;j++){  
                scanf("%d",&arrayA[j]);  
            }  
            //第二个数组  
            for(j = 0;j < lenb;j++){  
                scanf("%d",&arrayB[j]);  
            }  
            scanf("%d %d %d %d",&a,&b,&c,&d);  
            //合并数组  
            index = 0;  
            //第一个数组第a个数到第b个数  
            for(j = a-1;j < b;j++){  
                arrayC[index++] = arrayA[j];  
            }  
            //第二个数组的第c个数到第d个数  
            for(j = c-1;j < d;j++){  
                arrayC[index++] = arrayB[j];  
            }  
            //中间值  
            printf("%d\n",arrayC[(index-1)/2]);  
        }  
    }  
    return 0;  
}  

作者提醒

本题求的是中间值不是中位数,不用排序。