1.验证表

题目

成绩: 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. 5↵ 2. 20120001 zhangli↵ 3. 20120002 wanglei↵ 4. 20120003 duyang↵ 5. 20120004 lixin↵ 6. 20120005 liufan↵ 7. 3↵ 8. 20120001 zhangli↵ 9. 20120006 lixin↵ 10. 20120002 wanglei↵ 以文本方式显示 1. 20120006 lixin is not in LIST1.↵ 1秒 64M 0
测试用例 2 以文本方式显示 1. 5↵ 2. 20120001 zhangli↵ 3. 20120002 wanglei↵ 4. 20120003 duyang↵ 5. 20120004 lixin↵ 6. 20120005 liufan↵ 7. 3↵ 8. 20120001 zhangli↵ 9. 20120004 wanglei↵ 10. 20120006 duyang↵ 以文本方式显示 1. 20120004 wanglei is not in LIST1.↵ 2. 20120006 duyang is not in LIST1.↵ 1秒 64M 0
测试用例 3 以文本方式显示 1. 5↵ 2. 20120001 zhangli↵ 3. 20120002 wanglei↵ 4. 20120003 duyang↵ 5. 20120004 lixin↵ 6. 20120005 liufan↵ 7. 0↵ 以文本方式显示 1. the LIST2 is NULL.↵ 1秒 64M 0
测试用例 5 以文本方式显示 1. 5↵ 2. 20120001 zhangli↵ 3. 20120002 wanglei↵ 4. 20120003 duyang↵ 5. 20120004 lixin↵ 6. 20120005 liufan↵ 7. 3↵ 8. 20120002 wanglei↵ 9. 20120001 zhangli↵ 10. 20120004 lixin↵ 以文本方式显示 1. the records of LIST2 are all in the LIST1.↵ 1秒 64M 0

C++完整代码

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>
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<Student> 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<Student> 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;

}
相关推荐
DataFunTalk几秒前
Foundation Agent:深度赋能AI4DATA
前端·后端·算法
不是AI23 分钟前
【Java编程】【计算机视觉】一种简单的图片加/解密算法
java·算法·计算机视觉
明月看潮生36 分钟前
青少年编程与数学 02-016 Python数据结构与算法 23课题、分布式算法
分布式·python·算法·青少年编程·编程与数学
冠位观测者43 分钟前
【Leetcode 每日一题】2176. 统计数组中相等且可以被整除的数对
数据结构·算法·leetcode
幼儿园园霸柒柒1 小时前
第七章:7.2求方程a*x*x+b*x+c=0的根,用3个函数,分别求当:b*b-4*a*c大于0、等于0和小于0时的根并输出结果。从主函数输入a、b、c的值
c语言·开发语言·算法·c#
不知道叫什么呀1 小时前
【C语言基础】C++ 中的 `vector` 及其 C 语言实现详解
c语言·开发语言·c++
阳洞洞1 小时前
leetcode 213. House Robber II
算法·leetcode·动态规划
梭七y1 小时前
【力扣hot100题】(099)寻找重复数
算法·leetcode·职场和发展
小媛早点睡2 小时前
贪心算法day11(用最少数量的箭引爆气球)
算法·贪心算法
飞天狗1112 小时前
数据结构——二叉树
数据结构·算法