您的位置:首页 > 其它

PAT1019. 两个有序序列的中位数(25)

2014-10-15 15:33 323 查看
已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A0, A1…AN-1的中位数指A(N-1)/2的值,即第[(N+1)/2]个数(A0为第1个数)。

输入格式说明:

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

输出格式说明:

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

样例输入与输出:
序号输入输出
1
5
1 3 5 7 9
2 3 4 5 6

4

2
6
-100 -10 1 1 1 1
-50 0 2 3 4 5

1

3
3
1 2 3
4 5 6

3

4
34 5 6
1 2 3

3

5
12
1

1

提交代

#include<stdio.h>

#include<stdlib.h>

struct node

{

int num;

struct node *next;

};

int n,m;

struct node *creat(void)

{

struct node *head,*p1,*p2;

head=NULL;

int i=0;

for(i=0;i<n;i++)

{

p1=(struct node*)malloc(sizeof(struct node));

scanf("%d",&m);

p1->num=m;

if(head==NULL)

head=p1;

else

p2->next=p1;

p2=p1;

}

p1->next=NULL;

return head;

}

void find(struct node *p1,struct node *p2)

{

struct node *p3,*p4,*p5,*p6;

p3=p1,p4=p2;

int i=0;

while(p3!=NULL&&p4!=NULL)

{

if(p3->num>p4->num)

{

i++;

if(i==n)

{

printf("%d",p4->num);

break;

}

p4=p4->next;

}

if(p3->num<p4->num)

{

i++;

if(i==n)

{

printf("%d",p3->num);

break;

}

p3=p3->next;

}

if(p3->num==p4->num)

{

if(i==n)

{

printf("%d",p3->num);

break;

}

i=i+2;

p4=p4->next;

p3=p3->next;

}

}

printf("\n");

}

int main()

{

struct node *p1,*p2;

scanf("%d",&n);

p1=creat();

p2=creat();

find(p1,p2);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: