我试图通过使用实际的单词和len()函数来解决这个问题。我不断得到21224,但答案是21124.有人可以解释为什么吗?这是问题。Project Euler Prob 7 Python
如果数字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)
尝试当我在Python 2.7.11使用你的代码,它说num2words不存在。也许它只适用于python 3?我不想用这种方式。 –
pip安装num2words –