C++代码找出朋友圈中“帅到没有朋友”的人
思路:
首先输入每个朋友圈的人数和ID,然后将所有ID存入一个map中。接着输入待查询的人数和ID,判断其是否为孤独的人,如果是则存到set中,最后将所有太帅的人输出。
代码:
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main() {
int N, K, M, ID;
map<int, int> friends;
set<int> handsome;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> K;
for (int j = 0; j < K; j++) {
cin >> ID;
friends[ID]++;
}
}
cin >> M;
for (int i = 0; i < M; i++) {
cin >> ID;
if (friends[ID] == 1) {
handsome.insert(ID);
}
}
if (handsome.empty()) {
cout << "No one is handsome";
} else {
for (auto it = handsome.begin(); it != handsome.end(); ++it) {
if (it != handsome.begin()) {
cout << ' ';
}
cout << *it;
}
}
cout << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oWWS 著作权归作者所有。请勿转载和采集!