1042 字符统计 (20 分)

题目链接:1042 字符统计 (20 分)

 

这道题目比较简单。但是需要注意:

1、题目要求统计字母不按照大小写且输出小写。

2、字符串中包含空格,所以要用getline(cin,str)操作。

3、map<char,int>中是红黑树,所以会按照第一个键值的升序排列。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     string str;
 7     getline(cin,str);
 8     int max=0;
 9     char c;
10     map<char,int> m;
11     for(int i=0;i<str.length();i++)
12     {
13         if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
14         {
15             str[i]=tolower(str[i]);
16             m[str[i]]++;
17         }
18     }
19     map<char,int>::iterator it=m.begin();
20     for(;it!=m.end();it++)
21     {
22         if(it->second>max)
23         {
24             max=it->second;
25             c=it->first;
26         }
27     }
28     cout<<c<<" "<<max<<endl;
29 }
View Code

 

posted @ 2019-01-20 22:39  focusDing  阅读(278)  评论(0编辑  收藏  举报