题解 | #字符统计#

字符统计

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;
}

全部评论

相关推荐

面向对象的火龙果很爱...:去吃一顿炸鸡就走
点赞 评论 收藏
分享
05-23 20:31
已编辑
武汉大学 Java
内向的柠檬精在研究求...:注意把武大标粗标大 本地你俩不是乱杀
实习进度记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务