题解 | #名字的漂亮度#

名字的漂亮度

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define Maxsize 10005

int myCompare(const void *item1, const void *item2){
    int *a = (int *)item1, *b = (int *)item2;
    return *b - *a;
}

int main()
{
    int i, j, n;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        char str[Maxsize];
        scanf("%s", str);
        int array[26], len = strlen(str);
        memset(array, 0, sizeof(int) * 26);
        for(j = 0; j < len; j++){
            if(str[j] >= 'a' && str[j] <= 'z'){
                array[str[j] - 'a']++;
            }
            else if(str[j] >= 'A' && str[j] <= 'Z'){
                array[str[j] - 'A']++;
            }
        }
        qsort(array, 26, sizeof(int), myCompare);
        int beauty = 0;
        for(j = 0; j < 26; j++){
            if(array[j]){
                beauty += array[j] * (26 - j);
            }
        }
        printf("%d\n", beauty);
    }
    return 0;
}

全部评论

相关推荐

晗江雪:其实我只是觉得你们导员说的很好笑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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