PKU ACM 2418 Hardwood Species http://acm.pku.edu.cn/JudgeOnline/problem?id=2418

这道题过的贼诡异,我依旧用map容器做的,在自己的电脑上根本运行不了,但是交上去就ac了,太神奇了,map容器的主要特点就是代码少但是所需的运行时间长,如果能很好的掌握map类的所有成员函数而利用它,对于很多问题的解决都会事半功倍,很多函数不懂,还得继续努力啊
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string,int> mymap;    //创建map类对象
string str;
map<string,int>::iterator it;   //创建迭代指针
int count=0;
    while(getline(cin,str))  
{
   mymap[str]++;
   count++;
} 
for ( it=mymap.begin() ; it != mymap.end(); it++ )       //按顺序输出容器中的元素
{
cout<<it->first;
    printf(" %.4lf\n",100.00000000*(it->second)/(double)count);
}
return 0;
}

//mymap中的int成员默认值为0;所以没有必要考虑是否为空。

posted on 2011-05-06 19:17  _Clarence  阅读(147)  评论(0编辑  收藏  举报

导航