解题思路:利用STL unordered_set保存集合A和C, list保存集合B, 判断集合B中也在A中但不在C中的元素。
注意事项:题意要求按B输入顺序输出,所以需要用有序列表保存B中的元素。
参考代码:
#include<iostream> #include<string> #include<unordered_set> #include<list> using namespace std; int main() { int A, B, C; while (cin >> A >> B >> C) { string s; unordered_set<string> a; while (A--) { cin >> s; a.insert(s); } list<string> b; while (B--) { cin >> s; b.push_back(s); } unordered_set<string> c; while (C--) { cin >> s; c.insert(s); } bool spy = false; for (string s : b) { if (a.find(s) != a.end() && c.find(s) == c.end()) { cout << s << ' '; spy = true; } } if (!spy) cout << "No enemy spy"; cout << '\n'; } return 0; }
0.0分
0 人评分
大神老白 (C语言代码)浏览:715 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:619 |
买不到的数目 (C++代码)浏览:868 |
【绝对值排序】 (C语言代码)浏览:717 |
Pascal三角 (C语言代码)浏览:1189 |
【计算球体积】 (C语言代码)浏览:1103 |
蛇行矩阵 (C语言代码)浏览:536 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:1072 |
1054题解浏览:467 |
1050题解(结构体数组与结构体指针的使用)浏览:1110 |