题解 | #字母统计#

字母统计

https://www.nowcoder.com/practice/de7bf0945c1c4bd1aa9d49573b831f3c

  • 这道题直接让我回到了几年前学大计基在主楼机房敲代码的那个午后
  • 感觉方法应该是最简的了
#include <iostream>
using namespace std;
int main() {
    int chs[26]={0};//直接用数组下标表示字符在字母表中的位置
    string str;
    while(cin>>str){
        for(int i=0;i<str.length();i++){
            if(str[i]>='A'&&str[i]<='Z') chs[str[i]-'A']++;
        }
        for(int i=0;i<26;i++){
            cout<<char('A'+i)<<':'<<chs[i]<<endl;
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务