projecteuler---->problem=22----Names scores

本文详细介绍了如何下载并处理一个包含超过五千个姓氏的46KB文本文档,通过将其按字母顺序排列,计算每个姓氏的得分,并最终求得所有得分的总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sing 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,这个46KB的文本文档包含超过五千个姓氏,你要先把它们按字母表顺序排列好,然后把姓氏中每个字母在字母表中的索引加总,最后将所有索引的和乘以该姓氏在列表中的索引,就得到该姓氏的分值了。

例如,假定你已经把姓氏列表排好了,找到一个姓氏叫Colin,它的字母索引总和为3 + 15 + 12 + 9 + 14 = 53,已经它是第938个姓氏,于是它的分值就是938 × 53 = 49714。

你知道所有名字的总分值是多少吗?

import fileinput,string
f=open("22.txt","r")
s=f.readline()
info=s.split(',')
info.sort()
resu=0
for i in range(0,len(info)):
    addsum=0
    for j in range(0,len(info[i])):
        if info[i][j]>='A' and info[i][j] <= 'Z':
            addsum+=ord(info[i][j])-ord('A')+1
    addsum*=(i+1)
    resu+=addsum
print resu


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值