题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
//哈希表存储字符出现次数,并计算最大值;通过字符循环('0'~'z'保证了ascii顺序)判断字符个数等于max,<max,...
#include <stdio.h>
#include <string.h>
int main() {
char str[1000] = {0};
int hash[127] = {0};
while (scanf("%s", str) != EOF) { // 注意 while 处理多个 case
int len = strlen(str);
int max = 0;
for(int i=0; i<len; i++)
{
hash[str[i]]++;
max = (max > hash[str[i]] ? max : hash[str[i]]);
}
for(int i=max; i>0; i--)
{
for(char j = '0'; j<'z'+1; j++) //通过字符循环判断字符个数等于max,<max,...
{
if(hash[j] == i)
{
printf("%c",j);
}
}
}
printf("\n");
}
return 0;
}
