成绩 | 10 | 开启时间 | 2021年09月20日 星期一 15:50 |
折扣 | 0.8 | 折扣时间 | 2021年09月30日 星期四 23:59 |
允许迟交 | 否 | 关闭时间 | 2021年10月17日 星期日 23:59 |
应用中有时需要验证来自不同地方的两个表的信息是否一致。本实验编写具有如下功能的程序:输入两个学生记录表 LIST1 和 LIST2,在表 LIST2 中找出所有没有在表 LIST1 中出现的学生记录(设表LIST1为基础数据表,非空)。
每一个学生记录元素包含两个数据项:学号(整数),姓名;
如果学生记录表LIST2中的记录都包含在LIST1中,则输出the records of LIST2 are all in the LIST1.
如果学生记录表LIST2中的存在学号,姓名不能与表LIST1完全匹配的记录,则输出 学号(%8d)姓名(%15s)is not in LIST1.
如果LIST2为空表,则输出the LIST2 is NULL.
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 2 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 3 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 5 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
代码
还是两份,学长+我的。
我的代码配合注释应该很好懂,最近事情比较多,不多解释了。
学长代码
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
typedef struct stu{
long id;
char name[15];
struct stu *next;
}stu;
int main(){
stu *head,*p,*q;
int n;
int sum=0,flag=0;
scanf("%d",&n);
head=(stu*)malloc(sizeof(stu));
head->next=NULL;
q=head;
for(int i=0;i<n;i++){
p=(stu*)malloc(sizeof(stu));
for(int j=0;j<15;j++){
(*p).name[j]='\0';
}
scanf("%ld %s",&p->id,&p->name);
p->next=NULL;
q->next=p;
q=q->next;
}
scanf("%d",&n);
if(n==0){
printf("the LIST2 is NULL.\n");
}
else{
for(int i=0;i<n;i++){
p=(stu*)malloc(sizeof(stu));
flag=0;
for(int j=0;j<15;j++){
(*p).name[j]='\0';
}
q=head;
scanf("%ld %s",&p->id,&p->name);
do{
q=q->next;
if(q->id==p->id&&strcmp(q->name,p->name)==0){
flag=1;
sum++;
break;
}
}while(q->next!=NULL);
if(flag==0){
printf("%8d %s is not in LIST1.\n",p->id,p->name);
}
}
if(sum==n){
printf("the records of LIST2 are all in the LIST1.\n");
}
}
}
我的代码
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
typedef struct student{
int id;
char name[15];
struct student *next;
}stu;
void create(stu *list,int len){
for(int i=0;i<len;i++){
scanf("%d %s",&list->id,&list->name);
list->next=(stu*)malloc(sizeof(stu));
list=list->next;
}
}
void search(stu *p1,stu *p2,int len1,int len2){
int num_in=0;//list2在list1中的元素个数
stu *list2=p2;
for(int i=0;i<len2;i++){//list2中每一个元素 ,在list1中找相同
stu *list1=p1;
int flag=0;//找到相同了没有
//对比list1
if(list2->id==list1->id&&strcmp(list2->name,list1->name)==0){
//假如list2的当前元素和list1的第一个元素相同
num_in++;
list2=list2->next;//开始从list2下一个找起
continue;//不用对比了,直接下一个
}
else{//否则依次找list1的其他元素
for(int j=1;j<len1;j++){
list1=list1->next;
if(list2->id==list1->id&&strcmp(list2->name,list1->name)==0){//找到相同了
num_in++;
flag=1;
continue;//不用对比了,直接下一个
}
}
if(flag==0){//自始至终没有找到相同
printf("%d %s is not in LIST1.\n",list2->id,list2->name);
}
}
list2=list2->next;
}
if(num_in==len2){
printf("the records of LIST2 are all in the LIST1.\n");
}
}
int main(){
stu *list1,*list2;
list1=(stu*)malloc(sizeof(stu));
list2=(stu*)malloc(sizeof(stu));
int len1;
scanf("%d",&len1);
create(list1,len1);
int len2;
scanf("%d",&len2);
if(len2==0){
printf("the LIST2 is NULL.\n");
return 0;
}
create(list2,len2);
search(list1,list2,len1,len2);
}