题解 | 名字的漂亮度

名字的漂亮度

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//漂亮度的结构体
typedef struct Beauty
{
    char c;
    int cnt;
    int Bvalue;
}B;

//比较函数
int cmp(const void* a, const void* b )
{
    B* a1=(B*) a;
    B* b1=(B*) b;
    return b1->cnt-a1->cnt;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t)//进行T组数据的测试
    {
        char s[10001];
        scanf("%s",s);
        int len=strlen(s);
        //算每个字母出现的次数
        B beauty[26]={0};
        for(int i=0;i<len;i++)
        {
            beauty[s[i]-'a'].c=s[i];
            beauty[s[i]-'a'].cnt++;
        }

        //排序
        qsort(beauty, 26, sizeof(B), cmp);

        //定义漂亮度
        for(int i=0;i<26;i++)
        {
            beauty[i].Bvalue=26-i;
        }

        //计算漂亮度
        int max_beauty=0;
        for(int i=0;i<26;i++)
        {
            max_beauty+=beauty[i].Bvalue*beauty[i].cnt;
        }

        printf("%d\n",max_beauty);
        t--;//不要忘记外循环了
    }

    return 0;
}

全部评论

相关推荐

04-22 15:13
已编辑
Java
点赞 评论 收藏
分享
06-03 15:32
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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