九度OJ 题目1097:取中值

/********************************* 
 *    日期: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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值