输入一个由小写字母组成的字符串，输出出现最多次数的字母和出现的次数(Enter a string composed of lowercase letters, and output the letters that appear the most times and the number of occurrences)-其他

输入一个由小写字母组成的字符串，输出出现最多次数的字母和出现的次数(Enter a string composed of lowercase letters, and output the letters that appear the most times and the number of occurrences)

#include <iostream>

#include<string>

using namespace std;

int c[26];//存字母出现的次数

int main() {

string a;

cin>>a;

int max=0;

int i;

char x;//存出现次数最多的字母

for(i=0;i<a.length();i++)

c[a[i]-97]++;//a[i]是一个字符，从a到z，要将其映射到0-26存入数组c中记出现次数

for(i=0;i<26;i++)

{

if(c[i]>max){

max=c[i];

x=i+97;//可改写为‘a’ }

}

cout<<x<<endl;

cout<<max<<endl;

return 0; }

————————

#include <iostream>

#include<string>

using namespace std;

int c[26];// Number of occurrences of stored letters

int main() {

string a;

cin>>a;

int max=0;

int i;

char x;// Save the most frequent letters

for(i=0;i<a.length();i++)

c[a[i]-97]++;// A [i] is a character from a to Z. it should be mapped to 0-26 and stored in array C. the number of occurrences should be recorded

for(i=0;i<26;i++)

{

if(c[i]>max){

max=c[i];

x=i+97;// Can be rewritten as’ a ‘}

}

cout<<x<<endl;

cout<<max<<endl;

return 0; }