map容器
来源:互联网 发布:安装ubuntu时怎么分区 编辑:程序博客网 时间:2024/05/17 06:58
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
5greenredblueredred3pinkorangepink0
redpink
解法一:模拟
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct J//结构体的定义{ int a; char b[20];} s[1005];int cmp(J x,J y)//自定义cmp函数,用来排序{ return x.a>y.a;}int main(){ int t; while(~scanf("%d",&t)&&t) { int k=0; for(int i=0;i<1005;i++) s[i].a=0; for(int i=0; i<t; i++)//两层for循环很关键 { scanf("%s",s[k].b); for(int j=0; j<k; j++) { if(!strcmp(s[k].b,s[j].b)) { s[j].a++; break; } } s[k++].a++; } sort(s,s+t,cmp); printf("%s\n",s[0].b); }}解法二:map容器:#include <iostream>#include <string>#include <iterator>#include <map>#include<stdio.h>using namespace std;map<string,int>mapp;char s[30];int main(){ int t; while(~scanf("%d",&t)&&t) { mapp.clear(); int sum=0; string sum1; for(int i=0;i<t;i++) { scanf("%s",s); mapp[s]++; } for(map<string,int>::iterator iter=mapp.begin();iter!=mapp.end();iter++) { if(iter->second>sum) { sum1=iter->first; sum=iter->second; } } cout<<sum1<<endl; }}
阅读全文
0 0
- Map容器
- map容器
- map容器
- map容器
- map容器
- map容器
- map容器
- 容器map
- map容器
- map容器
- map容器
- map容器
- Map容器
- map容器
- map容器
- map容器
- { 容器map }C++ <map>
- map容器即关联容器
- Java中的 流
- 常见的七种排序算法解析
- SVN未提交代码被覆盖或者删除
- IO模型详解
- 11、python中的try/except/else/finally语句
- map容器
- 【免费】领取阿里云官方服务器代金券
- Android应用或界面启动时间性能
- NOIP-2017
- [私人手册]Linux命令
- MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
- tf生成随机数
- tp5框架paginate的使用
- CycleGAN的原理与实验详解