题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream> #include<string> using namespace std; int getid(char c) { int id=0; if(c-'a'>=0 && 'z'-c>=0)id=c-'a'; else if(c-'A'>=0 && 'Z'-c>=0)id=c-'A'; else if(c-'0'>=0 && '9'-c>=0)id=c-'0'+26; else id=36; return id; } int main() { string s; int cnt[36]={0}; char p; getline(cin,s); cin>>p; cin>>p; cin>>s>>p; for(int i=0;i<s.length();i++){ int id=getid(s[i]); cnt[id]++; } cout<<cnt[getid(p)]; return 0; } // 64 位输出请用 printf("%lld")