题解 | #名字的漂亮度#

名字的漂亮度

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

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

void count(char c, int cnt[]) {

if(c >= 'a' && c <= 'z')
{
    cnt[c-'a']++;
}
if(c >= 'A' && c <= 'Z')
{
    cnt[c-'A']++;
}

}

int main(void) { int n = 0; char str[10000]; char ptr = NULL; int cnt[26]; int i = 0, j =0; int temp = 0; int ret = 0; while(scanf("%d", &n) != EOF) { for(i = 0; i < n; i++) { scanf("%s", str); memset(cnt, 0, 426); ret = 0; ptr = str; while(*ptr) { count(*ptr, cnt); ptr++; }

        for(i = 0; i < 26; i++)
        {
            for(j = 0; j < 26; j++)
            {
                if(cnt[i] < cnt[j])
                {
                    temp = cnt[i];
                    cnt[i] = cnt[j];
                    cnt[j] = temp;
                }
            }
        }
        
        for(i = 0; i < 26; i++)
        {
            //printf("cnt[%d]:%d\n", i, cnt[i]);
            ret = ret + cnt[i] * (i + 1);
        }
        
        printf("%d\n", ret);
    }
    
    
}


return 0;

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务