【C语言】名字漂亮度(牛客,华为机试)

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3?tpId=37&tqId=21268&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title= 其实不难发现,只要把出现次数最多的字符,出现次数记录下来,让这个个数*26,第二多的个数*25,依次*24.......

//beauty
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int com(void* p1, void* p2)
{
     return (strcmp((int*)p2, (int*)p1)); //accordance the number it appeared,descending
}
int main() {
    int N = 0;  //line number 
    int map[128] = { 0 }; //store character ASCII to compute the number it appear 
    int count = 26; //it should be descending,the largest number multiply 26,second multiply 25... 
    int i = 0; //record the number of character strings 
    int j = 0; //index to search in each character
    char* tmp = NULL; //record the place of the character in map
    int ans[10000] = { 0 }; //store the ans number ,finally printf
    char* name[1000];//store character string
    while (scanf("%d", &N) != EOF)
    {
        for (i = 0; i < N; i++)
        {
            name[i] = malloc(sizeof(char) * 10000);
            if (!name[i])
            {
                return -1;
            }
            scanf("%s", name[i]);
        }

        for (i = 0; i < N; i++)
        {
            memset(map, 0, sizeof(map));
            count = 26;
            tmp = name[i];
            int len = strlen(tmp);
            for (j = 0; j < len; j++)
            {
                map[tmp[j]]++;
            }
            qsort(map, 128, sizeof(int), com);
            for (j = 0; j < 26; j++)
            {
                ans[i] += (count * map[j]);
                count--;
            }
            free(name[i]); //Don't forget!!
            name[i] = NULL;
        }
        for (i = 0; i < N; i++)
        {
            printf("%d\n", ans[i]);
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值