nefuoj-549:气球
2016-02-16 23:39
369 查看
description |
<span style="font-family:SimSun;font-size:14px;"> 长达5个小时的ACM比赛结束了,每组参赛者桌子前都挂了不同颜色的气球,裁判们想知道哪种颜色的气球最多,请你写个程序帮他们计算一下。 </span> |
input |
<span style="font-family:SimSun;font-size:14px;"> 输入数据有多组,每组数据第一行是一个数字N(N<100),即气球的总数。接下来是N行,每一行包含一种颜色,颜色是一个字符串,长度不超过15。当N是0的时候,测试结束。</span> |
output |
<span style="font-family:SimSun;font-size:14px;">对于每组输入数据,输出一行,即数量最多的那种颜色。</span> |
sample_input |
<span style="font-family:SimSun;font-size:14px;">5 green red blue red red 3 pink orange pink 0</span> |
sample_output |
<span style="font-family:SimSun;font-size:14px;">red pink</span> |
但是水平不够没能写出来,于是咨询了两个学长,学长告诉我用暴力+数组的方法就可以写出来,也就是定义一个二维字符数组,但是我更喜欢string'类型,于是就有了下边的代码;
code1:
#include <string>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=105;
int cnt[maxn];
int main()
{
int n;
while(cin>>n&&n)
{
memset(cnt,0,sizeof cnt);
string s;
string str[maxn];
int num=0;
while(n--)
{
cin>>s;
bool flag=0;
for(int i=0; i<num; i++)
{
if(str[i]==s)
{
cnt[i]++;
flag=1;
break;
}
}
if(!flag)
{
str[num++]=s;
cnt[num]=1;
}
}
int maxn=cnt[0],it=0;
for(int i=0; i<num; i++)
{
if(maxn<cnt[i])
{
maxn=cnt[i];
it=i;
}
}
cout<<str[it]<<endl;
}
return 0;
}
下边的代码是李学长帮忙写的,用的map,我的用map的思路没错,但是能力不够。还是多谢学长和学长们的帮助了,林大的学长们真的很棒;mark一下代码:
code2:
#include <map>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int cmp(pair<string,int> x,pair<string,int> y){
return x.second>y.second;
}
int main(){
int n,i,j;
string s;
while(scanf("%d",&n)!=EOF&&n){
map<string,int> m;
map<string,int>::iterator ite;
vector<pair<string,int> > v;
for(i=0;i<n;i++){
cin>>s;
m[s]++;
}
for(ite=m.begin();ite!=m.end();ite++)
v.push_back(make_pair(ite->first,ite->second));
sort(v.begin(),v.end(),cmp);
cout<<v[0].first<<endl;
}
return 0;
}
相关文章推荐
- 在程序中添加广告
- UVA 297
- 蓝桥杯 未名湖边的烦恼 (算法训练)
- 使用ServerSocketChannel与SocketChannel实现的CS模式
- xwiki 7.14 Windows安装教程(jetty + MySQL5.7+jdk1.7)
- UI基础-05 HelloWorld 工程中的故事板文件
- jquery on()方法和bind()方法的区别
- Java中 ==和equals方法的区别
- windows 10 安装CUDA 7.5和PyCUDA(在GPU上运行theano)
- 学习笔记5
- leetcode 160:Intersection of Two Linked Lists
- CA服务器的创建和CA客户端认证过程
- 【Unity】是否进入摄像机显示区域。
- hdoj 4497(gcd&lcm分解质因数)
- centos6.5集群lvs+keepalived部署
- 继承AppCompatActivity如何实现无标题
- leetcode:Roman to Integer
- 用ccproxy + stunnel做个加密代理
- objective-C学习 自定义对象归档 archiver2
- cocos2dx 3.4配置vs2013 + lua环境