PAT (Advanced Level) 1029. Median (25)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1029. Median (25)相关的知识,希望对你有一定的参考价值。

scanf读入居然会超时...用了一下输入挂才AC...

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=1000000+10;
int a[maxn],b[maxn];
int n1,n2;

inline bool scan_d(int &num)  
{
        char in;bool IsN=false;
        in=getchar();
        if(in==EOF) return false;
        while(in!=-&&(in<0||in>9)) in=getchar();
        if(in==-){ IsN=true;num=0;}
        else num=in-0;
        while(in=getchar(),in>=0&&in<=9){
                num*=10,num+=in-0;
        }
        if(IsN) num=-num;
        return true;
}

int MIN(int a,int b)
{
    if(a<b) return a;
    return b;
}

int main()
{
    scanf("%d",&n1);
    for(int i=1;i<=n1;i++)  scan_d(a[i]);
    scanf("%d",&n2);
    for(int i=1;i<=n2;i++)  scan_d(b[i]);
    
    int sz=0; int p1=1,p2=1;
    a[n1+1]=999999999,b[n2+1]=999999999;

    while(sz<n1+n2)
    {
        sz++;
        int tmp1=a[p1],tmp2=b[p2];
        if(tmp1<tmp2) p1++; else p2++;
        if(sz==(n1+n2+1)/2)
        {
            printf("%d\n",MIN(tmp1,tmp2));
            break;
        }
    }
    return 0;
}

 

以上是关于PAT (Advanced Level) 1029. Median (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced 1029 Median (25分)

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095