P8833 [传智杯 #3 决赛] 课程 ----- 集合求交集
题目背景
disangan233 喜欢数数,于是他想让你帮他回答一个问题。
题目描述
传智专修学院提供 A,BA,B 两个课程,分别有 n,mn,m 个学生报名。报名 AA 的学生的编号为 a_nan,报名 BB 的学生的编号为 b_mbm,求有多少个学生同时报名了两个课程。
对于所有数据,n,m,a_i,b_i\leq 20n,m,ai,bi≤20,保证每个课程报名的学生编号不会重复。
输入格式
输入共 33 行。
第 11 行输入 22 个正整数 n,mn,m。
第 22 行输入 nn 个正整数 a_1\ldots a_na1…an,表示报名课程 AA 的学生编号。
第 33 行输入 mm 个正整数 b_1\ldots b_mb1…bm,表示报名课程 BB 的学生编号。
学生编号不保证从小到大排序。
输出格式
输出共 11 行 11 个整数,表示答案。
输入输出样例
输入 #1
5 5 1 2 3 4 5 1 3 4 5 6
输出 #1
4
#include <iostream> #include<iomanip> #include <math.h> #include <vector> #include <unordered_set> using namespace std; int main() { int n, m; int stu_a, stu_b; int res = 0; cin >> n >> m; unordered_set<int> a, b; while (n--) { cin >> stu_a; a.insert(stu_a); } while (m--) { cin >> stu_b; b.insert(stu_b); } for (auto && i = a.begin(); i != a.end(); ++i) { //cout << " " << *i << endl; if (b.count(*i)) res++; } cout << res; return 0; }
hello my world
本文来自博客园,作者:slowlydance2me,转载请注明原文链接:https://www.cnblogs.com/slowlydance2me/p/16917735.html

浙公网安备 33010602011771号