PAT 1029. Median (25)
2018-03-09 13:17
441 查看
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
Sample Output
13
思路:考虑两个序列长度和的奇偶性,还有,由于频繁进行输入,用scanf,不用cin
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<long int> v;
int n1,n2;
cin>>n1;
for(int i = 0; i < n1; i++)
{
long int tmp;
scanf("%ld",&tmp);
v.push_back(tmp);
}
cin>>n2;
for(int i = 0; i < n2; i++)
{
long int tmp;
scanf("%ld",&tmp);
v.push_back(tmp);
}
sort(v.begin(),v.end());
if(v.size() % 2)
cout<<v[v.size() / 2]<<endl;
else
cout<<v[v.size() / 2 - 1]<<endl;
return 0;
}
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,不用cin
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<long int> v;
int n1,n2;
cin>>n1;
for(int i = 0; i < n1; i++)
{
long int tmp;
scanf("%ld",&tmp);
v.push_back(tmp);
}
cin>>n2;
for(int i = 0; i < n2; i++)
{
long int tmp;
scanf("%ld",&tmp);
v.push_back(tmp);
}
sort(v.begin(),v.end());
if(v.size() % 2)
cout<<v[v.size() / 2]<<endl;
else
cout<<v[v.size() / 2 - 1]<<endl;
return 0;
}
相关文章推荐
- PAT (Advanced Level)1029. Median (25)
- PAT甲级 1029. Median (25)
- 1029. Median (25)-PAT
- 【PAT】1029. Median (25)
- pat 1029. Median (25)
- pat甲级1029. Median (25)
- 1029. Median (25)-PAT甲级真题(two points)
- PAT甲 1029. Median (25)
- PAT 1029. Median (25) ZZZ想太多系列
- 1029. Median (25)-PAT甲级真题
- 浙大 PAT Advanced level 1029. Median (25)
- PAT (Advanced Level) 1029. Median (25)
- PAT 1029. Median (25)
- 【C++】PAT(advanced level)1029. Median (25)
- PAT 1029. Median (25) 求2个排序数组的中位数——剔除法
- 1029. Median (25) @ PAT (Advanced Level) Practise
- PAT-A-1029. Median (25)
- 1029. Median (25)【排序】——PAT (Advanced Level) Practise
- PAT(甲级)1029. Median (25)
- PAT (Advanced Level) Practise 1029. Median (25)