L2-019 悄悄关注 (25分)

本文介绍了一种算法,通过分析用户在社交媒体上的关注列表和点赞行为,推测可能被悄悄关注的人。利用C++中的set和map数据结构,筛选出点赞次数超过平均值且未在关注列表中出现的用户,以此揭示隐藏的关注关系。

新浪微博上有个“悄悄关注”,一个用户悄悄关注的人,不出现在这个用户的关注列表上,但系统会推送其悄悄关注的人发表的微博给该用户。现在我们来做一回网络侦探,根据某人的关注列表和其对其他用户的点赞情况,扒出有可能被其悄悄关注的人。

输入格式:
输入首先在第一行给出某用户的关注列表,格式如下:

人数N 用户1 用户2 …… 用户N
其中N是不超过5000的正整数,每个用户i(i=1, …, N)是被其关注的用户的ID,是长度为4位的由数字和英文字母组成的字符串,各项间以空格分隔。

之后给出该用户点赞的信息:首先给出一个不超过10000的正整数M,随后M行,每行给出一个被其点赞的用户ID和对该用户的点赞次数(不超过1000),以空格分隔。注意:用户ID是一个用户的唯一身份标识。题目保证在关注列表中没有重复用户,在点赞信息中也没有重复用户。

输出格式:
我们认为被该用户点赞次数大于其点赞平均数、且不在其关注列表上的人,很可能是其悄悄关注的人。根据这个假设,请你按用户ID字母序的升序输出可能是其悄悄关注的人,每行1个ID。如果其实并没有这样的人,则输出“Bing Mei You”。

输入样例1:
10 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao
8
Magi 50
Pota 30
LLao 3
Ammy 48
Dave 15
GAO3 31
Zoro 1
Cath 60
输出样例1:
Ammy
Cath
Pota
输入样例2:
11 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao Pota
7
Magi 50
Pota 30
LLao 48
Ammy 3
Dave 15
GAO3 31
Zoro 29
输出样例2:
Bing Mei You

这道题的重点在于找到第二次与第一次输入不重复的名字,可以使用stl中的set和map来解决,用set来作为关注的人的容器,用map来记录没有关注的却点赞的人和点赞数,以人名为下标,输出超过平均值的map数组。重点操作在于查重时候利用find函数,set里如果find函数找不到指定的值,则会返回end的迭代器,因此可以通过这个判定来把重复的名字筛掉。况且这两种容器底层均为红黑树,因此不用考虑人名排序问题。

#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include <iomanip>
#include<map>
#include<set>
using namespace std;
int main()
{
	map<string,int> people;
	set<string> temp;
	string name;
	int num;
	int n;
	int sum = 0;
	cin >> n;
	while (n--)
	{
		cin >> name;
		temp.insert(name);
	}
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> name >> num;
		sum += num;
		if (temp.find(name) == temp.end())
			people[name] = num;
	}
	sum /= n;
	int flag = 1;
	for (auto it = people.begin(); it != people.end(); it++)
	{
		if (it->second > sum)
		{
			cout << it->first << endl;
			flag = 0;
		}
	}
	if(flag)
		cout << "Bing Mei You" << endl;
	return 0;
}
编译器 C (gcc) 内存 544 / 65536 KB 用时 1 / 400 ms 状态 答案错误 数 0 / 15 评测时间 2025/07/13 11:25:30 评测详情 测试点 提示 内存(KB) 用时(ms) 结果 得 sample 544 1 答案错误 0 / 2 sample2 316 1 答案错误 0 / 2 sample3 344 1 答案错误 0 / 2 sample4 304 1 答案错误 0 / 3 sample5 304 1 答案错误 0 / 3 sample6 304 1 答案错误 0 / 3 提交代码 复制内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <stdio.h> #include <string.h> int main() { int n; scanf("%d", &n); int candies[10000], new_candies[10000]; for (int i = 0; i < n; i++) { scanf("%d", &candies[i]); } int rounds = 0; while (1) { int changed = 0; memset(new_candies, 0, sizeof(new_candies)); // 配糖果 for (int i = 0; i < n; i++) { int give = candies[i] / 3; int remain = candies[i] - 2 * give; if (i == 0) { new_candies[n - 1] += give; new_candies[1] += give; new_candies[i] = remain; } else if (i == n - 1) { new_candies[n - 2] += give; new_candies[0] += give; new_candies[i] = remain; } else { new_candies[i - 1] += give; new_candies[i + 1] += give; new_candies[i] = remain; } } 编译器输出 a.c: In function ‘main’: a.c:6:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 6 | scanf("%d", &n); | ^~~~~~~~~~~~~~~ a.c:10:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 10 | scanf("%d", &candies[i]); | ^~~~~~~~~~~~~~~~~~~~~~~~仍旧0
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值