我试图通过使用实际的单词和len()函数来解决这个问题。我一直得到21224,但答案是21124。谁能解释一下原因吗?问题出在这里。
如果数字1到5是用单词写出的:1、2、3、4、5,那么总共使用3+3+5+4+4=19个字母。
如果从1到1000 (包括1000)的所有数字都是用文字写出来的,那么将使用多少个字母?
注意:不要计算空格或连字符。例如,342 (342)包含23个字母,115 (115)包含20个字母。在写出数字时使用"and“是符合英国用法的。
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)发布于 2016-11-18 08:26:58
检查一下你四十的拼写。正确的拼写方法是‘40’而不是‘40 to’。
发布于 2016-11-18 18:28:59
你可以试试这个
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)https://stackoverflow.com/questions/40670219
复制相似问题