题解 | #名字的漂亮度#

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        char name[10001];
        scanf("%s", name);
        // printf("%s\n",name);
        int len = 0;
        while(name[len] != '\0') len++;
        // printf("the len is %d\n", len);
        int frequency[26] = {0};
        // 计算频率
        for(int a = 0; a < len; a++) {
            frequency[name[a] - 'a']++;
            // printf("%c count++\n", name[a]);
        }
        // 之后从大到小加漂亮度
        int total = 0;
        for(int a = 0; a < 26; a++) {
            // printf("frequency of letter %c is %d\n", a + 'a', frequency[a]);
        }

        for(int x = 0; x < 26; x++) {
            int maxindex = 0;
            for(int y = 0; y < 26; y++) {
                if(frequency[maxindex] < frequency[y]) {
                    maxindex = y;
                }
            }
            // printf("update max freq to %d, index is %d\n", frequency[maxindex], maxindex);
            total += frequency[maxindex] * (26 - x);
            // printf("total is %d\n", total);
            frequency[maxindex] = 0;
        }
        printf("%d\n", total);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务