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