应用中有时需要验证来自不同地方的两个表的信息是否一致。本实验编写具有如下功能的程序:输入两个学生记录表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>
struct Student
{
int id;
char name[50];
};
int compareStudents(const void* a, const void* b)
{
return ((struct Student*)a)->id - ((struct Student*)b)->id;
}
int main() {
struct Student list1[100], list2[100], notInList1[100];
int n, i,j,m, p = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d %s", &list1[i].id, list1[i].name);
scanf("%d", &m);
for (i = 0; i < m; i++) scanf("%d %s", &list2[i].id, list2[i].name);
if (m == 0) {
printf("the LIST2 is NULL.\n");
return 0;
}
qsort(list1, n, sizeof(struct Student), compareStudents);
qsort(list2, m, sizeof(struct Student), compareStudents);
for (i = 0; i < m; i++)
{
int found = 0;
for (j = 0; j < n; j++)
{
if (list2[i].id == list1[j].id && strcmp(list2[i].name, list1[j].name) == 0) {
found = 1;
break;
}
}
if (!found) notInList1[p++] = list2[i];
}
if (!p) printf("the records of LIST2 are all in the LIST1.\n");
else
{
for (i = 0; i < p; i++) printf("%d %s is not in LIST1.\n", notInList1[i].id, notInList1[i].name);
}
return 0;
}