您的位置:首页 > 其它

PAT两个有序链表序列的合并

2018-03-20 12:21 411 查看

7-51 两个有序链表序列的合并(20 分)

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的并集新非降序链表S3。

输入格式:

输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:

在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出
NULL

输入样例:

1 3 5 -1
2 4 6 8 10 -1

输出样例:

1 2 3 4 5 6 8 10
这个题看完后给人的感觉就是用链表硬解比较麻烦,事实上也是代码冗长,操作复杂,但是实用性较强,可以通过所有测试点,获得满分,下面是c语言下的链表解法#include "stdio.h"
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node, *linkList;

void InitList(linkList *L)
{
(*L) = (linkList)malloc(sizeof(Node));
(*L)->next = NULL;
}

void creat(linkList L)
{
Node *s, *r = L;
int num, flag = 1;
while(flag)
{
scanf("%d", &num);
if(num >= 0) //num
{
s = (linkList)malloc(sizeof(Node));
s->data = num;
r->next = s;
r = r->next;
}
else
{
flag = 0;
r->next = NULL;
}
}
}

void print(Node * L)
{
Node * t = L->next;
int firstNum = 1;
if(t == NULL) printf("NULL\n");
while(t != NULL)
{
if(firstNum)
{
printf("%d", t->data);
firstNum = 0;
}
else
printf(" %d", t->data);
t = t->next;
}
}

void sort(Node *L1, Node *L2, Node *L3)
{
Node *s1 = L1->next, *s2 = L2->next;
Node *r = L3;
while(s1 != NULL && s2 != NULL)
{
if(s1->data < s2->data)
{
r->next = s1;
r = r->next;
s1 = s1->next;
}
else
{
r->next = s2;
r = r->next;
s2 = s2->next;
}
}
if(s1 != NULL)
r->next = s1;
else
r->next = s2;

// return s3;
}

int main(int argc, char const *argv[])
{
Node *L1, *L2, *L3;
InitList(&L1);
InitList(&L2);
InitList(&L3);
creat(L1);
creat(L2);
sort(L1, L2, L3);
print(L3);
return 0;
}这段代码是网上摘下来的,自己之前写过类似的,但不完全一样
下面是测试结果

2018/3/20 12:12:42答案正确207-51C (gcc)481 ms 
测试点结果耗时内存
0答案正确1 ms128KB
1答案正确1 ms132KB
2答案正确2 ms132KB
3答案正确481 ms38648KB
这个题目简单粗暴的方法就是两个数组模拟链表,然后c++sort排序#include<bits/stdc++.h>
using namespace std;
int main()//有重复元素版
{
int a[10000];
int b[10000];
int t,s,n1=0,n2=0;

scanf("%d",&t);
int i=0;
while(t!=-1)
{
a[i]=t;
i++;
n1=i;
scanf("%d",&t);
}
getchar();

scanf("%d",&s);
int j=0;
while(s!=-1)
{
b[j]=s;
j++;
n2=j;
scanf("%d",&s);
}
if(n1+n2!=0)
{

for(i=0;i<n2;i++)
{
a[n1+i]=b[i];

}
sort(a,a+n1+n2);
for(i=0;i<n1+n2;i++)
{
if(i==n1+n2-1)
{
printf("%d",a[i]);
}
else
printf("%d ",a[i]);
}

}
else
printf("NULL");

}测试结果如下

0答案正确2 ms256KB
1答案正确2 ms256KB
2答案正确2 ms248KB
3段错误6 ms352KB
第四个测试点有错,现在仍未找到问题
我找改程序测试点的时候衍生出了第二个版本,即两链表和并后无重复元素的版本,虽然背离的不明确的题意但是误打误撞发现了一个新的c++函数unique(a,a+n1+n2)可以去除数组中的重复元素,实际上并没有删除,而是移动到了数组最后的位置
下面是代码#include<bits/stdc++.h>
//#include<ctype.h>
using namespace std;
int main()
{
int a[10000];
int b[10000];
int t,s,n1=0,n2=0;

scanf("%d",&t);
int i=0;
while(t!=-1)
{
a[i]=t;
i++;
n1=i;
scanf("%d",&t);
}
getchar();

scanf("%d",&s);
int j=0;
while(s!=-1)
{
b[j]=s;
j++;
n2=j;
scanf("%d",&s);
}

for(i=0;i<n2;i++)
{
a[n1+i]=b[i];

}
if(n1+n2!=0)
{
sort(a,a+n1+n2);//将整形数组按非递减排序
unique(a,a+n1+n2);//除去数组中重复的元素

int N=0;//用作计数器
for(i=0;i<n1+n2;i++)
{
if(isalnum(a[i]%10+'0')&&a[i]>a[i-1])//调用isalnum()函数判断该字符的ascii码是不是字母或数字,整形值必须加上'0'
{
N++;

}
else
break;
}
for(i=0;i<N;i++)
{
if(i==N-1)
{
printf("%d",a[i]);//按要求输出,结尾不带空格
}
else
printf("%d ",a[i]);
}

}

else
printf("NULL");

}测试的结果

0答案正确2 ms248KB
1答案错误2 ms256KB
2答案正确2 ms384KB
3段错误6 ms248KB
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表合并 PAT PTA