赞
踩
5 3 1 3 5 6 9 2 4 10
1 2 3 4 5 6 9 10
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 100000//注意边界要比给定的范围大一些,否则会RUN TIME
typedef struct
{
int *elem;
int length;
int listsize;
}qlist;
//初始化线性表
void Initlist(qlist &L)
{
L.elem=new int [MAXSIZE];
L.length=0;
L.listsize=MAXSIZE;
}
//创建线性表
void Creatlist(qlist &L,int n)
{
for(int i=0;i<n;i++)
{
scanf("%d",&L.elem[i]);
}
L.length=n;
}
//归并线性表
void Doublelist(qlist &L1,qlist &L2,qlist &L3)
{
int i,j;
i=j=0;
int k=-1;
while(i<L1.length&&j<L2.length)
{
if(L1.elem[i]<=L2.elem[j])//注意此处是“<=”,不要丢了“=”
{
L3.elem[++k]=L1.elem[i];
i++;
}
else
{
L3.elem[++k]=L2.elem[j];
j++;
}
}
if(i<L1.length)
{
for(int t=i;t<L1.length;t++)
{
L3.elem[++k]=L1.elem[t];
}
}
else if(j<L2.length)
{
for(int t=j;t<L2.length;t++)
{
L3.elem[++k]=L2.elem[t];
}
}
L3.length=L1.length+L2.length;
//printf("%d\n",L3.length);
}
//输出归并后的线性表
void Putlist(qlist &L)
{
for(int i=0;i<L.length;i++)
{
if(i==L.length-1)
{
printf("%d\n",L.elem[i]);
}
else
{
printf("%d ",L.elem[i]);
}
}
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
qlist L1,L2,L3;
Initlist(L1);
Initlist(L2);
Initlist(L3);
Creatlist(L1,n);
Creatlist(L2,m);
Doublelist(L1,L2,L3);
Putlist(L3);
return 0;
}

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。