Euler Prob 7 Python项目

2024-06-28 20:51:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过使用实际单词和len()函数来解决这个问题。我一直得到21224,但答案是21124。有人能解释一下为什么吗?问题来了。你知道吗

如果数字1到5是用文字写出来的:1,2,3,4,5,那么总共有3+3+5+4+4=19个字母。你知道吗

如果所有从1到1000(包括1000)的数字都用文字写出来,会用多少个字母?你知道吗

注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。写数字时使用“和”符合英国用法。你知道吗

one_nine='onetwothreefourfivesixseveneightnine'
ten_nineteen='teneleventwelvethirteenfourteenfifteensixteenseventeeneighteennineteen'
twenty_ninety_byten='twentythirtyfourtyfiftysixtyseventyeightyninety'
one_ninetynine_list=[one_nine*9,ten_nineteen,twenty_ninety_byten*10]
one_ninetynine=''.join(one_ninetynine_list)

onehundred_ninehundred_byonehundred_list=[one_nine,'hundred'*9]
onehundred_ninehundred_byonehundred=''.join(onehundred_ninehundred_byonehundred_list)
one_onethousand_list=[one_ninetynine*10,onehundred_ninehundred_byonehundred*100,'and'*891,'onethousand']
one_onethousand=''.join(one_onethousand_list)
print len(one_onethousand)

Tags: len字母数字onelist文字joinnine
2条回答

你可以试试这个

from num2words import num2words

list = []
for i in range(1, 1001):
    list.append(num2words(i, lang='en_GB'))

letters = 0
for element in list:
    for letter in element:
        if 97 <= ord(letter) <= 122:
            letters += 1

print(letters)

检查你的四十字拼写。正确的拼写方法是四十而不是四十。你知道吗

相关问题 更多 >