PAT1026

来源:互联网 发布:钻石篮球联赛官网数据 编辑:程序博客网 时间:2024/05/17 09:25

题目链接:
pat1029(A)
本题开始的想法是使用快速排序求出中位数,后来发现两组数都已经升序,只需找到固定序号的元素即可,AC代码如下:

#include <stdio.h> #define MAX 999999999999999999999;long post1[10000006];long post2[10000006];int main(){    int m,n,i,j;    scanf("%d",&m);    for(i=0;i<m;i++)        scanf("%ld",&post1[i]);    post1[m]=MAX;    scanf("%d",&n);    for(i=0;i<n;i++)        scanf("%ld",&post2[i]);    post2[n]=MAX;    int mu;    if((n+m)%2==0) mu=(n+m)/2;    else mu=(n+m+1)/2;    int p1=0,p2=0,num=0;    while(true){        if(post1[p1]>post2[p2]){            num++;            if(num==mu) {                printf("%ld",post2[p2]);                break;            }            p2++;        }        else{            num++;            if(num==mu){                printf("%ld",post1[p1]);                break;            }            p1++;        }    }    return 0;}
0 0