题目:1042 字符统计 (20 分)
来源:PAT (Basic Level) Practice
传送门 1042 字符统计
题面
思路:字符串输入,for循环比较一遍即可。
Code
点击查看代码
#include
using namespace std;
int ans[300], Max = -1;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
string s; char str;
getline(cin, s);
for (int i = 0; i < s.size(); i++) {
s[i] = tolower(s[i]);
ans[s[i] - '0']++;
if (s[i] >= 'a' && s[i] <= 'z') {
if (ans[s[i] - '0'] > Max || (ans[s[i] - '0'] == Max && s[i] < str)) {
Max = ans[s[i] - '0'];
str = s[i];
}
}
}
cout << str << " " << Max << "\n";
return 0;
}
}