成绩: 10 / 折扣: 0.8
应用中有时需要验证来自不同地方的两个表的信息是否一致。本实验编写具有如下功能的程序:输入两个学生记录表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
- #include
- #include
- using namespace std;
-
- struct Student {
- int id;
- string name;
- };
-
- bool compareStudents(const Student& s1, const Student& s2) {
- return s1.id < s2.id;
- }
-
- int main() {
- vector
list1, list2; -
- //输入表LIST1的学生记录
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- Student student;
- cin >> student.id;
- cin >> student.name;
- list1.push_back(student);
- }
-
- //输入表LIST2的学生记录
- cin >> n;
- for (int i = 0; i < n; i++) {
- Student student;
- cin >> student.id;
- cin >> student.name;
- list2.push_back(student);
- }
- if (list2.empty()) {
- cout << "the LIST2 is NULL." << endl;
- return 0;
- }
-
- // 对表LIST1和表LIST2进行排序
- sort(list1.begin(), list1.end(), compareStudents);
- sort(list2.begin(), list2.end(), compareStudents);
-
- // 循环遍历两个表的学生记录并比较
- vector
notInList1; - for (const Student& student2 : list2) {
- bool found = false;
-
- for (const Student& student1 : list1) {
- if (student2.id == student1.id && student2.name == student1.name) {
- found = true;
- break;
- }
- }
- if (found == false) {
- notInList1.push_back(student2);
- }
- }
-
- // 输出结果
- if (notInList1.empty()) {
- cout << "the records of LIST2 are all in the LIST1." << endl;
- }
- else {
- for (const Student& student : notInList1) {
- cout << student.id << " " << student.name << " is not in LIST1." << endl;
- }
- }
-
- return 0;
-
- }