题解 | #计算某字母出现次数#
计算某字母出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include<iostream> #include<map> using namespace std; map<char, int> char2num; int main(){ char a; char c; for(int i=0;i<500;i++){ cin.get(c); if (c == '\n') break; else if(c <= 'Z' && c >= 'A'){ if(char2num.find(c) == char2num.end()) char2num[c] = 1; else char2num[c] ++; } else if(c <= 'z' && c >= 'a'){ if(char2num.find(c-32) == char2num.end()) char2num[c-32] = 1; else char2num[c-32] ++; }else{} } cin>>a; if(a <= 'Z' && a >= 'A'){ // 大写 if(char2num.find(a) != char2num.end()){ cout<<char2num[a]; }else cout<<0; }else{ // 小写 if(char2num.find(a-32) != char2num.end()){ cout<<char2num[a]+char2num[a-32]; }else cout<<0; } return 0; }