PAT 1029. Median (25)
2015-08-07 23:47
525 查看
1029. Median (25)
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 nondecreasing 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.
Input
Each input file contains 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.
Output
For each test case you should output the median of the two given sequences in a line.
Sample Input
4 11 12 13 14 5 9 10 15 16 17
Sample Output
13 输入需要采用scanf输入
#include <iostream> #include <cstdio> using namespace std; long seq1[1000000], seq2[1000000]; long finalseq[2000000]; int main() { int n1, n2; cin >> n1; for (int i = 0; i < n1; i++) scanf("%ld", &seq1[i]); cin >> n2; for (int i = 0; i < n2; i++) scanf("%ld", &seq2[i]); int i = 0, j = 0, k = 0; while (i < n1 || j < n2) { if (i == n1) //only seqence1 finalseq[k++] = seq2[j++]; else if (j == n2) //only seqence2 finalseq[k++] = seq1[i++]; else if (seq1[i] < seq2[j]) finalseq[k++] = seq1[i++]; else if (seq1[i]>seq2[j]) finalseq[k++] = seq2[j++]; else { finalseq[k++] = seq1[i++]; j++; } } cout << finalseq[(k - 1) / 2]; }
相关文章推荐
- jquey操作select
- HTML学习笔记(3)——CSS的引入,选择器和文本样式
- 添加Hyper-V群集主机到VMM服务器
- CSS字体设置
- Hive 之再认识
- playsms+kannel 配置过程中常见错误总结(1)
- JavaScript 编程
- C语言函数-指针
- unity代码反混淆
- 不见面的情侣要做到的七件事
- VMM服务器主机和主机组介绍
- AutoCAD常用快捷键
- python实现RSA加密算法
- combination
- 修改主题记
- 添加工作组Hyper-V主机到VMM服务器
- HDU 4358 Boring counting(树状数组)
- net程序破解神器
- buffers
- 添加Hyper-V主机到VMM服务器