码迷,mamicode.com
首页 > 其他好文 > 详细

1042 字符统计 (20 分)

时间:2019-01-20 23:38:49      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:tar   std   ref   length   set   键值   分享   http   分享图片   

题目链接: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

 

1042 字符统计 (20 分)

标签:tar   std   ref   length   set   键值   分享   http   分享图片   

原文地址:https://www.cnblogs.com/ManOK/p/10296500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!