题解 | #字符统计#

字符统计

https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

#include <stddef.h>
#include <stdio.h>
#include <string.h>
int main() {
    int i, j, len, n = 0;
    unsigned char ch;
    char line[1001];
    short ci[256];
    short temp;
    memset(&ci, 0, sizeof(ci));
    scanf("%s", line);
    len = strlen(line);
    for (i = 0; i < len; i++) {
        ch = line[i];
        ci[ch] += 0x100;
        ci[ch] |= (0x100 - ch);
    }
    for (i = '0'; i <= 'z'; i++) {
        if (ci[i] != 0) {
            ci[n++] = ci[i];
        }
    }
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (ci[i] < ci[j]) {
                temp = ci[i];
                ci[i] = ci[j];
                ci[j] = temp;
            }
        }
    }
    for (i = 0; i < n; i++)
        printf("%c", 0x100 - (ci[i] & 0xff));
    return 0;
}
// 代码还可以再环保一点,懒得改了

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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