寻找水王2

随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有三个发帖很多的ID。据统计他们的发帖数量超过了1/4,你能从发帖列表中快速找到他们吗?

 

设计思路:这次还是选择hash表,觉得这个用起来效率并不是很差,而且代码更加简洁,更加易懂:

 1 #include<iostream>
 2 #include<map>
 3 #include<string>
 4 using namespace std;
 5 int main(){
 6     int n;
 7     map<string,int> hash;
 8     cin>>n; //帖子的总数量
 9     string id,ans[10];
10     int x=0;
11     for(int i=0;i<n;i++){
12         cin>>id;
13         if(hash.count(id)==0){
14             hash[id]=1;
15         }else {
16             hash[id]++;
17         }
18         if(hash[id]>n/4){
19             ans[x++]=id;
20         }
21     }
22     if(x==0)cout<<"不存在水王!"<<endl;
23     else {
24         for(int i=0;i<x;i++){
25             cout<<"水王"<<i<<""<<ans[i]<<endl; 
26         }    
27     } 
28     return 0;
29 } 

转载于:https://www.cnblogs.com/yifan2016/p/5609459.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值