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个非降序排列的整数。数字用空格间隔。
输出格式说明:
在一行中输出两个输入序列的并集序列的中位数。
样例输入与输出:
提交代码
#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;
}
输入格式说明:
输入分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 | 3 |
5 | 12 | 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;
}
相关文章推荐
- 2-13. 两个有序序列的中位数(25)(ZJU_PAT 链表 | 数组 )
- pat 两个有序序列的中位数
- 7-19 两个有序序列的中位数(25 分)
- Programming Ability Test学习 2-13. 两个有序序列的中位数(25)
- 7-1 两个有序序列的中位数(25 分)
- 浙大PAT 2-13. 两个有序序列的中位数 (解题思路)
- 7-1 两个有序序列的中位数(25 分)
- PAT 2-13 两个有序序列的中位数(C语言实现)
- 2-13. 两个有序序列的中位数(25)
- PAT (Advanced Level) 1029. Median (25) 求两个有序数组的中位数,二分
- 浙江大学PAT上机题解析之2-13. 两个有序序列的中位数
- 两个有序序列的中位数(25 分)
- PAT 两个有序序列的中位数
- 2-13. 平均两个有序序列(25)(ZJU_PAT 名单 | 排列 )
- 2-13. 两个有序序列的中位数(25)
- PAT两个有序链表序列的合并
- 5-7 两个有序序列的中位数 (25分)
- 两个有序序列的中位数
- 浙大PAT 2-11. 两个有序链表序列的合并 (解题思路)
- 2-11. 两个有序链表序列的合并(15) (ZJU_PAT 链表 | vector)