九度OJ 题目1097:取中值

来源:互联网 发布:mac复制粘贴文件 编辑:程序博客网 时间:2024/05/29 15:10
/*********************************  *    日期:2013-2-5 *    作者:SJF0115  *    题号: 九度OJ 题目1097:取中值 *    来源:http://ac.jobdu.com/problem.php?pid=1097 *    结果:AC  *    来源:2009年上海交通大学计算机研究生机试真题 *    总结:本题求的是中间值不是中位数,不用排序。**********************************/ #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;}


/*********************************  *    日期:2013-2-5 *    作者:SJF0115  *    题号: 九度OJ 题目1097:取中值 *    来源:http://ac.jobdu.com/problem.php?pid=1097 *    结果:AC  *    来源:2009年上海交通大学计算机研究生机试真题 *    总结:本题求的是中间值不是中位数,不用排序。**********************************/ #include<stdio.h>#include<stdlib.h>#include<string.h>int arrayA[1000001];int arrayB[1000001];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);int n1 = (b - a + 1);int n2 = (d - c + 1);int index = (n1 + n2 - 1) / 2;//在[a b]区间if(index < n1){index = index + a - 1;printf("%d\n",arrayA[index]);}//在[c d]区间else{index = index - n1 + c - 1;printf("%d\n",arrayB[index]);}}    }    return 0;}