编写一个C++代码微博上有个点赞功能你可以为你喜欢的博文点个赞表示支持。每篇博文都有一些刻画其特性的标签而你点赞的博文的类型也间接刻画了你的特性。然而有这么一种人他们会通过给自己看到的一切内容点赞来狂刷存在感这种人就被称为点赞狂魔。他们点赞的标签非常分散无法体现出明显的特性。本题就要求你写个程序通过统计每个人点赞的不同标签的数量找出前3名点赞狂魔。输入格式:输入在第一行给出一个正整数N≤100是待
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
struct User {
string name;
unordered_map<int, int> tags;
};
bool cmp(const pair<string, double>& a, const pair<string, double>& b) {
return a.second < b.second;
}
int main() {
int N;
cin >> N;
vector<User> users(N);
for (int i = 0; i < N; i++) {
cin >> users[i].name;
int K;
cin >> K;
for (int j = 0; j < K; j++) {
int tag;
cin >> tag;
users[i].tags[tag]++;
}
}
vector<pair<string, double>> counts;
for (const User& user : users) {
double count = 0;
for (const auto& p : user.tags) {
count += p.second;
}
counts.push_back(make_pair(user.name, count));
}
sort(counts.begin(), counts.end(), cmp);
int num = min(3, N);
for (int i = 0; i < num; i++) {
cout << counts[i].first;
if (i != num - 1) {
cout << " ";
}
}
for (int i = 0; i < 3 - num; i++) {
cout << "-";
if (i != 3 - num - 1) {
cout << " ";
}
}
return 0;
}
``
原文地址: http://www.cveoy.top/t/topic/h5pe 著作权归作者所有。请勿转载和采集!