注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Minary_Acdream

http://f10.moe/

 
 
 

日志

 
 

[hdu]1004 Let the Balloon Rise  

2012-07-29 16:32:21|  分类: HDU |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
第一次看到这道题的时候觉得麻烦就没做,当接触了STL后,就觉得其实还挺简单的。

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43385    Accepted Submission(s): 15279


Problem Description
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. 
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 

Sample Input
5 green red blue red red 3 pink orange pink 0
 

Sample Output
red pink
 

Author
WU, Jiazhi
 

Source
 

Recommend
JGShining
 

题目的意思就是输出每组数据里面出现最多的颜色。我们可以用map来做。
下面是代码:


#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;

bool cmp(pair<string,int>a,pair<string,int>b)
{
return a.second < b.second;
}
int main()
{
int T;
while(cin>>T)
{
if(T == 0) break;
map <string,int> color;
for(int i = 0;i < T;i ++)
{
string color_;
cin>>color_;
color[color_]++;
}
map<string,int>::iterator it = max_element(color.begin(),color.end(),cmp);
cout<<it->first<<endl;

}
return 0;

}



第一次用max_element,就顺便来讲一下这个。(http://stl123.net/algorithm/max_element/)

max_element算法返回区间[first,last)中的最大元素
有如下两个使用原形,查找迭代器区间[first,last)的最大元素,如果区间为空,则返回last。
函数原型:


template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator largest = first;
if (first==last) return last;
while (++first!=last)
if (*largest<*first) // or: if (comp(*largest,*first)) for the comp version
largest=first;
return largest;
}



参数

first, last
指出要查找的迭代器区间[first,last)
comp

用比较函数对象comp作为参数,比较迭代器区间[first , last)上的元素,如果第一个元素小于第二个元素,返回true,否则返回false。


返回值

指向区间最大元素值的迭代器

示例代码




// min_element/max_element
#include <iostream>
#include <algorithm>
using namespace std;

bool myfn(int i, int j) { return i<j; }

struct myclass {
bool operator() (int i,int j) { return i<j; }
} myobj;

int main () {
int myints[] = {3,7,2,5,6,4,9};

// using default comparison:
cout << "The smallest element is " << *min_element(myints,myints+7) << endl;
cout << "The largest element is " << *max_element(myints,myints+7) << endl;

// using function myfn as comp:
cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl;

// using object myobj as comp:
cout << "The smallest element is " << *min_element(myints,myints+7,myobj) << endl;
cout << "The largest element is " << *max_element(myints,myints+7,myobj) << endl;

return 0;
}



输出:



The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9





  评论这张
 
阅读(423)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018