欧拉计划22题

Names score

Using names.txt(right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3+15+12+9+14=53, is the 938th name in the list. So, COLIN would obtain a score of 938×53=49714.

What is the total of all the name scores in the file?

英文名得分

在文本文件names.txt中有五千多个英文名。首先将这些英文名按照字母序排序,然后计算出每个英文名的字母价值,乘以它在按字母序排序后的位置,得到的就是这个英文名的得分。

例如,按照字母序排序后,位于第938位的名字是COLIN,它的字母价值是3+15+12+9+14=53。因此,COLIN这个英文名的得分是938×53=49714。

上述文本文件中,所有英文名的得分之和是多少?

文本如果上面的打不开就试试下面这个,这个题解,主要是排序,因为我用的是C语言,我直接选用的qsort函数进行排序,代码实现如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "22.h"
//我把文本直接放在头文件,用变量存上了,这样代码看着更简洁
int cmpare(const void *a, const void *b) {
    char *a1 = (char *)a;
    char *b1 = (char *)b;
    return strcmp(a1, b1);
}

int main() { 
    qsort(str, 5163, sizeof(str[0]), cmpare);//我直接用笨办法求出名字个数了
    for (int i = 0; i <= 5162; i++) printf("%s ", str[i]);
    long long ans = 0, sum = 0;
    for (int i = 0; i <= 5162; i++) {
        for (int j = 0; str[i][j]; j++) {
        sum += str[i][j] - 'A' + 1;
        }
        ans += sum * (i + 1);
        sum = 0;
    }
    printf("%lld\n", ans); 
    return 0;
}

 最终答案:871198282

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初猿°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值