题解 | #字符统计#

字符统计

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

全部评论

相关推荐

测试糕手手:社会第一课,随便吹牛逼,直接说四个月,别老实。老实人只会被欺负
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:05
点赞 评论 收藏
分享
下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
06-26 17:24
已编辑
宁波大学 golang
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 14:10
啊啊啊啊好幸福,妈妈是我找工作发疯前的一束光
榕城小榕树:你是我见过最幸福的牛客男孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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