姓名得分
在这个46K的文本文件names.txt(右击并选择“目标另存为……”)中包含了五千多个姓名。首先将它们按照字母序排列,然后计算出每个姓名的字母值,乘以它在按字母顺序排列后的位置,以计算出姓名得分。
例如,按照字母序排列后,位于第938位的姓名COLIN的字母值是3 + 15 + 12 + 9 + 14 = 53。因此,COLIN的姓名得分是938 × 53 = 49714。
文件中所有姓名的姓名得分之和是多少?
代码演示
#include <iostream>
#include <stdio.h>
#include <inttypes.h>
#include <algorithm>
#include "22.h"
int32_t main() {
int64_t i = 0, len, sum, total = 0;
while (nameList[i++].length() != 0) ;
std::sort(nameList, nameList + i - 1);
i = 0;
while (nameList[i].length() != 0) {
sum = 0;
for (int32_t j = 0; j < nameList[i].length(); j++) {
sum += nameList[i][j] - 'A' + 1;
}
total += sum * (++i);
}
printf("%"PRId64"\n", total);
return 0;
}