九度题目1004:Median
来源:互联网 发布:sql 求平均值 编辑:程序博客网 时间:2024/05/01 08:55
May 24, 2016
作者:dengshuai_super
出处:http://blog.csdn.net/dengshuai_super/article/details/51493053
声明:自由转载,转载请注明作者及出处。
题目1004:Median
题目描述: Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the non-decreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13. Given two increasing sequences of integers, you are asked to find their median.输入: Each input file may contain more than one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.输出: For each test case you should output the median of the two given sequences in a line.样例输入: 4 11 12 13 14 5 9 10 15 16 17样例输出: 13
题目分析:
找出两个数组所有元素中第medianNum个元素(非递减顺序),并记录该元素在数组一还是数组二中,以及在该数组中的位置,最后返回该元素。
代码如下:
/****************************************************************************** 九度题目1004:Median ******************************************************************************* by Deng shuai May 24 2016* http://blog.csdn.net/dengshuai_super******************************************************************************* Copyright (c) 2016, Deng Shuai* All rights reserved.*****************************************************************************/
#include<stdio.h>long int N,M; //两个数组元素个数long int array1[10]; //定义两个数组long int array2[10]; int main() { //分别输入两个数组元素个数和元素 while(scanf("%ld",&N)!=EOF) { int i,j; int medianNum,median,num = 0; for(i = 0;i<N;i++){ scanf("%ld",&array1[i]); } scanf("%ld",&M); medianNum = ((N+M)%2==0)?(N+M)/2:(N+M)/2+1; for(i = 0;i<M;i++){ scanf("%ld",&array2[i]); } for(i = 0,j = 0;i<N&&j<M;){ num++; //记录当前是第num个数(从小到大) if(array1[i]<array2[j]){ if(num == medianNum){ median = array1[i]; //找到则跳出循环 break; } i++; } else{ if(num == medianNum){ median = array2[j]; break; } j++; } } if(num == medianNum){ printf("%d\n",median); } else{ if(i < N){ printf("%ld\n",array1[medianNum - M - 1]); //把array2搜索完了还没让num=medianNum,说明中位数在array1中,(medianNum - M - 1)则为中位数在array1中的下标位置 } else{ printf("%ld\n",array2[medianNum - N - 1]); //把array1搜索完了还没让num=medianNum,说明中位数在array2中,(medianNum - N - 1)则为中位数在array2中的下标位置 } } } return 0; }
运行结果:
0 0
- 【九度】题目1004:Median
- 九度 题目1004:Median
- 九度题目1004:Median
- 九度——题目1004:Median
- 九度online:题目1004:Median
- 九度OJ--题目1004:Median
- 九度OJ题目1004:Median
- 九度OJ 题目1004:Median
- 九度 1004 Median
- 九度[1004]-Median
- 九度:1004<Median><中位数>
- 九度 OJ 1004 median
- 九度OJ 1004:Median
- 九度oj-1004-Median
- 九度 OJ 1004:Median
- 九度OJ : 1004 Median
- 九度OJ-1004:Median
- 【九度OJ】1004:Median
- 面试题 <第一个只出现一次的字符>(11)
- BZOJ 4571: [Scoi2016]美味
- List接口
- 一个IO传奇的一生-4
- 线段树入门总结
- 九度题目1004:Median
- 快速幂取模算法
- iOS开发之高级视图—— UITableView(一)简单例子
- 一个IO传奇的一生-5
- 一个IO传奇的一生-6
- 如何更换阿里云邮箱绑定手机号
- python学习06-条件和循环
- Django + Nginx + Uwsgi 配置服务器
- DGN格式转化为shp格式