Run ID:70893
提交时间:2024-04-20 14:36:14
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() { int cnt[26]; int max=0; int len; char str[1010];//字符串 char ch; memset(str,0,sizeof(str));//清零 memset(cnt,0,sizeof(cnt));//清零 gets(str);//获取字符串 len=strlen(str);//获取长度 for(int i=0; i<len; i++) {//计数 cnt[str[i]-'a']++;//把计数器此字符的ASCII码-a的ASCII的那一项加一 } for(int i=0; i<26; i++) {//找最大值 if(cnt[i]>max) {//用大于号做判断不用考虑输出ASCII小的那个,用大于等于就需要考虑 ch=(char)i+'a';//i+a的ASCII转成字符保存 max=cnt[i]; } } cout<<ch<<" "<<max; //输出出现次数最多的字符和该字符出现的次数 return 0; }