PAT

    xiaoxiao2021-03-25  109

    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

    分析:找出两个有序的数列的中位数。我用暴力做的,要讲cin cout改成scanf coutcode: #include<iostream> #include<cstdio> #include<vector> #include<algorithm> using namespace std; vector<long int>A; vector<long int>B; bool comp(long int a,long int b) { if(a<b) return true; else return false; } int main() { long int M; long int N; long int tmp; scanf("%ld",&M); for(long int i=0;i<M;i++) { scanf("%ld",&tmp); A.push_back(tmp); } scanf("%ld",&N); for(long int i=0;i<N;i++) { scanf("%ld",&tmp); A.push_back(tmp); } sort(A.begin(),A.end(),comp); B.push_back(A.at(0)); for(int i=1;i<A.size();i++) { if(B.at(B.size()-1)==A.at(i)) continue; B.push_back(A.at(i)); } if(B.size()%2==1) printf("%ld\n",B.at(B.size()/2)); else printf("%ld\n",B.at(B.size()/2-1)); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-7673.html

    最新回复(0)