PTA点赞狂魔

7-49 点赞狂魔 (25 分)
微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持。每篇博文都有一些刻画其特性的标签,而你点赞的博文的类型,也间接刻画了你的特性。然而有这么一种人,他们会通过给自己看到的一切内容点赞来狂刷存在感,这种人就被称为“点赞狂魔”。他们点赞的标签非常分散,无法体现出明显的特性。本题就要求你写个程序,通过统计每个人点赞的不同标签的数量,找出前3名点赞狂魔。

输入格式:
输入在第一行给出一个正整数N(≤100),是待统计的用户数。随后N行,每行列出一位用户的点赞标签。格式为“Name K F
​1
​​ ⋯F
​K
​​ ”,其中Name是不超过8个英文小写字母的非空用户名,1≤K≤1000,F
​i
​​ (i=1,⋯,K)是特性标签的编号,我们将所有特性标签从1到107编号。数字间以空格分隔。

输出格式:
统计每个人点赞的不同标签的数量,找出数量最大的前3名,在一行中顺序输出他们的用户名,其间以1个空格分隔,且行末不得有多余空格。如果有并列,则输出标签出现次数平均值最小的那个,题目保证这样的用户没有并列。若不足3人,则用-补齐缺失,例如mike jenny -就表示只有2人。

输入样例:
5
bob 11 101 102 103 104 105 106 107 108 108 107 107
peter 8 1 2 3 4 3 2 5 1
chris 12 1 2 3 4 5 6 7 8 9 1 2 3
john 10 8 7 6 5 4 3 2 1 7 5
jack 9 6 7 8 9 10 11 12 13 14
输出样例:
jack chris john

首先不知道107是10^7 然后打出运行超时 的桶排

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct note{
	char name[15];
	int cs=0;
	int sl;
}a[105];
int k[10000010];
int cmp(struct note a1,struct note a2){
	if(a1.cs ==a2.cs ) return a1.sl <a2.sl ;
	else return a1.cs >a2.cs ;
}
int main(){
	int n,m;
	scanf("%d",&n);
	int t=0,i;
	while(t<n){
		memset(k,0,sizeof(k));
		scanf("%s",a[t].name );
		scanf("%d",&m);	
		a[t].sl =m;
		int w;
	for(i=0;i<m;i++){
		scanf("%d",&w);
		k[w]++;
	}
	for(i=1;i<=10000000;i++){
		if(k[i]) a[t].cs ++;
	}

	t++;
	}

	sort(a,a+n,cmp);
	if(n>=3) printf("%s %s %s",a[0].name ,a[1].name ,a[2].name );
	else if(n==2) printf("%s %s -",a[0].name ,a[1].name);
	else if(n==1) printf("%s - -",a[0].name);
	else if (n==0) printf("- - -");
	
	return 0;
}

使用unique 函数去重;
sort(w,w+m);
a[t].cs =unique(w,w+m)-w;
判断不同类别的标签数;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct note{
	char name[10];
	int cs;
	int sl;
}a[105];

int cmp(struct note a1,struct note a2){
	if(a1.cs ==a2.cs ) return a1.sl <a2.sl ;
	else return a1.cs >a2.cs ;
}
int main(){
	int n,m;
	scanf("%d",&n);
	int t=0,i;
	while(t<n){
		int w[1000+10];
		memset(w,0,sizeof(w));
		scanf("%s",a[t].name );
		scanf("%d",&m);	
		a[t].sl =m;
	
	for(i=0;i<m;i++){
		scanf("%d",&w[i]);
	}
	sort(w,w+m);
	a[t].cs =unique(w,w+m)-w;
	t++;
	}

	sort(a,a+n,cmp);
	if(n>=3) printf("%s %s %s",a[0].name ,a[1].name ,a[2].name );
	else if(n==2) printf("%s %s -",a[0].name ,a[1].name);
	else if(n==1) printf("%s - -",a[0].name);
	else if (n==0) printf("- - -");
	
	return 0;
}

我不会用Set 写啊
通过 qq.insert(w)
a[t].cs =qq.size() ;
size值即为不同类型标签数

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
set<int> qq;
while(t<n){
		qq.clear();
		scanf("%s",a[t].name );
		scanf("%d",&m);	
		a[t].sl =m;
	int w; 
	for(i=0;i<m;i++){
		scanf("%d",&w);
		qq.insert(w);
	}
	a[t].cs =qq.size() ;
	t++;
	}

猜你喜欢

转载自blog.csdn.net/weixin_43880084/article/details/85206034
今日推荐