两个有序序列的中位数

已知有两个等长的非降序序列S1, S2, 设计函数求S1S2并集的中位数。有序序列A

0

,A

1

,,A

N−1

的中位数指A

(N−1)/2

的值,即第(N+1)/2个数(A

0

为第1个数)。

输入格式:

输入分三行。第一行给出序列的公共长度N0<N100000),随后每行输入一个序列的信息,即N个非降序排列的整数。数字用空格间隔。

输出格式:

在一行中输出两个输入序列的并集序列的中位数。

输入样例1:

5

1 3 5 7 9

2 3 4 5 6

输出样例1:

4

输入样例2:

6

-100 -10 1 1 1 1

-50 0 2 3 4 5

输出样例2:

1

#include<stdio.h>
#include<mm_malloc.h>
int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int main()
{
    int N,i,j;
    int *p,*q,*m;
    scanf("%d",&N);
    p=(int*)malloc(N*sizeof(int));
    q=(int*)malloc(N*sizeof(int));
    m=(int*)malloc(2*N*sizeof(int));
    for(i=0;i<N;i++)
    {
        scanf("%d",&p[i]);
    }
    for(j=0;j<N;j++)
    {
        scanf("%d",&q[j]);
    }
    for(i=0;i<N;i++)
    {
        m[i]=p[i];
    }
    for(i=0;i<N;i++)
    {
        m[i+N]=q[i];
    }
    qsort(m, 2*N, sizeof(int), cmp);
    printf("%d\n",m[(2*N+1)/2-1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41995348/article/details/80544813