输入一串字符,包含空格,统计出现最多的次数的字符,如果有相同的数目,按照ASCII的顺序,进行输出。输入样例:sa6kk66o#i5kd89y输出样例:6 3 k 3数据范围:字符长度不大于100
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
const int MAX_LEN = 100;
char n[MAX_LEN] = {0};
int nmax = 0;
int nmax2 = 0;
int timesA[26] = {0};
int timesa[26] = {0};
int times0[10] = {0};
cin.getline(n, MAX_LEN);
for (int i = 0; i < MAX_LEN; i++)
{
if (n[i] >= '0' && n[i] <= '9')
{
times0[n[i] - '0']++;
}
else if (n[i] >= 'a' && n[i] <= 'z')
{
timesa[n[i] - 'a']++;
}
else if (n[i] >= 'A' && n[i] <= 'Z')
{
timesA[n[i] - 'A']++;
}
else if (n[i] == '\0')
{
break;
}
}
//0
for (int i = 0; i < 10; i++)
{
if (nmax < times0[i])
{
nmax = times0[i];
}
}
//A
for (int i = 0; i < 26; i++)
{
if (nmax < timesA[i])
{
nmax = timesA[i];
}
}
//a
for (int i = 0; i < 26; i++)
{
if (nmax < timesa[i])
{
nmax = timesa[i];
}
}
for (int i = 0; i < 10; i++)
{
if (nmax == times0[i] && nmax > 0)
{
cout << i << ":" << times0[i] << endl;
}
}
for (int i = 0; i < 26; i++)
{
if (nmax == timesA[i] && nmax > 0)
{
cout << (char)(i + 'A') << ":" << timesA[i] << endl;
}
}
for (int i = 0; i < 26; i++)
{
if (nmax == timesa[i] && nmax > 0)
{
cout << (char)(i + 'a') << ":" << timesa[i] << endl;
}
}
return 0;
}