可能重复:
Project Euler 17
因此,对于这个项目euler的问题:
如果数字1到5用文字写出:一,二,三,四,五,那么总共有3 + 3 + 5 + 4 + 4 = 19个字母。
如果所有1到1000(一千)的数字都用文字写出来,会用多少个字母?
注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。在写出数字时使用“和”符合英国的用法。
我试图在没有帮助的情况下解决这个问题......但我认为我做错了什么......
我有这段代码......
def convert(n):
if n < 10:
return placement(n,1)
elif n == 10:
return "ten"
elif n > 10 and n < 20:
return teen(n)
elif n >= 20 and n < 100:
if int(str(n)[1]) == 0:
return placement(str(n)[0],2)
else:
return placement(str(n)[0],2)+convert(int(str(n)[1]))
elif len(str(n)) == 3:
x = ""
h = placement(str(n)[0], 3)
if int(str(n)[1]) == 0 and int(str(n)[2]) == 0:
return h
else:
z = int(str(n)[1]+"0")+int(str(n)[2])
x = h + "and" + str(convert(z))
return x
elif len(str(n)) == 4:
x = ""
t = placement(str(n)[0], 4)
if int(str(n)[1]) == 0 and int(str(n)[2]) == 0 and int(str(n)[3]) == 0:
return t
def teen(n):
n = int(n)
if n == 11:
return "eleven"
if n == 12:
return "twelve"
if n == 13:
return "thirteen"
if n == 14:
return "fourteen"
if n == 15:
return "fifteen"
if n == 16:
return "sixteen"
if n == 17:
return "seventeen"
if n == 18:
return "eighteen"
if n == 19:
return "nineteen"
else:
return ""
def placement(n,p):
n = int(n)
if p == 1:
if n == 1:
return "one"
if n == 2:
return "two"
if n == 3:
return "three"
if n == 4:
return "four"
if n == 5:
return "five"
if n == 6:
return "six"
if n == 7:
return "seven"
if n == 8:
return "eight"
if n == 9:
return "nine"
else:
return ""
if p == 2:
if n == 1:
return "ten"
if n == 2:
return "twenty"
if n == 3:
return "thirty"
if n == 4:
return "fourty"
if n == 5:
return "fifty"
if n == 6:
return "sixty"
if n == 7:
return "seventy"
if n == 8:
return "eighty"
if n == 9:
return "ninety"
else:
return ""
if p == 3:
if n != 0:
return placement(n,1) + "hundred"
else:
return ""
if p == 4:
if n != 0:
return placement(n,1) + "thousand"
else:
return ""
z = 0
for x in range(1,1001):
z += len(convert(x))
print z
这会吐出答案21224
,但项目欧拉表示错误,所以有人看到我在做什么错了吗?并且有没有人有更好的方法来做到这一点,如果你这样做,请解释一下,而不是只给我一些代码?
对不起这一点......但如果我打印每一行,我就会得到这个......
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twentyone
twentytwo
twentythree
twentyfour
twentyfive
twentysix
twentyseven
twentyeight
twentynine
thirty
thirtyone
thirtytwo
thirtythree
thirtyfour
thirtyfive
thirtysix
thirtyseven
thirtyeight
thirtynine
fourty
fourtyone
fourtytwo
fourtythree
fourtyfour
fourtyfive
fourtysix
fourtyseven
fourtyeight
fourtynine
fifty
fiftyone
fiftytwo
fiftythree
fiftyfour
fiftyfive
fiftysix
fiftyseven
fiftyeight
fiftynine
sixty
sixtyone
sixtytwo
sixtythree
sixtyfour
sixtyfive
sixtysix
sixtyseven
sixtyeight
sixtynine
seventy
seventyone
seventytwo
seventythree
seventyfour
seventyfive
seventysix
seventyseven
seventyeight
seventynine
eighty
eightyone
eightytwo
eightythree
eightyfour
eightyfive
eightysix
eightyseven
eightyeight
eightynine
ninety
ninetyone
ninetytwo
ninetythree
ninetyfour
ninetyfive
ninetysix
ninetyseven
ninetyeight
ninetynine
onehundred
onehundredandone
onehundredandtwo
onehundredandthree
onehundredandfour
onehundredandfive
onehundredandsix
onehundredandseven
onehundredandeight
onehundredandnine
onehundredandten
onehundredandeleven
onehundredandtwelve
onehundredandthirteen
onehundredandfourteen
onehundredandfifteen
onehundredandsixteen
onehundredandseventeen
onehundredandeighteen
onehundredandnineteen
onehundredandtwenty
onehundredandtwentyone
onehundredandtwentytwo
onehundredandtwentythree
onehundredandtwentyfour
onehundredandtwentyfive
onehundredandtwentysix
onehundredandtwentyseven
onehundredandtwentyeight
onehundredandtwentynine
onehundredandthirty
onehundredandthirtyone
onehundredandthirtytwo
onehundredandthirtythree
onehundredandthirtyfour
onehundredandthirtyfive
onehundredandthirtysix
onehundredandthirtyseven
onehundredandthirtyeight
onehundredandthirtynine
onehundredandfourty
onehundredandfourtyone
onehundredandfourtytwo
onehundredandfourtythree
onehundredandfourtyfour
onehundredandfourtyfive
onehundredandfourtysix
onehundredandfourtyseven
onehundredandfourtyeight
onehundredandfourtynine
onehundredandfifty
onehundredandfiftyone
onehundredandfiftytwo
onehundredandfiftythree
onehundredandfiftyfour
onehundredandfiftyfive
onehundredandfiftysix
onehundredandfiftyseven
onehundredandfiftyeight
onehundredandfiftynine
onehundredandsixty
onehundredandsixtyone
onehundredandsixtytwo
onehundredandsixtythree
onehundredandsixtyfour
onehundredandsixtyfive
onehundredandsixtysix
onehundredandsixtyseven
onehundredandsixtyeight
onehundredandsixtynine
onehundredandseventy
onehundredandseventyone
onehundredandseventytwo
onehundredandseventythree
onehundredandseventyfour
onehundredandseventyfive
onehundredandseventysix
onehundredandseventyseven
onehundredandseventyeight
onehundredandseventynine
onehundredandeighty
onehundredandeightyone
onehundredandeightytwo
onehundredandeightythree
onehundredandeightyfour
onehundredandeightyfive
onehundredandeightysix
onehundredandeightyseven
onehundredandeightyeight
onehundredandeightynine
onehundredandninety
onehundredandninetyone
onehundredandninetytwo
onehundredandninetythree
onehundredandninetyfour
onehundredandninetyfive
onehundredandninetysix
onehundredandninetyseven
onehundredandninetyeight
onehundredandninetynine
twohundred
twohundredandone
twohundredandtwo
twohundredandthree
twohundredandfour
twohundredandfive
twohundredandsix
twohundredandseven
twohundredandeight
twohundredandnine
twohundredandten
twohundredandeleven
twohundredandtwelve
twohundredandthirteen
twohundredandfourteen
twohundredandfifteen
twohundredandsixteen
twohundredandseventeen
twohundredandeighteen
twohundredandnineteen
twohundredandtwenty
twohundredandtwentyone
twohundredandtwentytwo
twohundredandtwentythree
twohundredandtwentyfour
twohundredandtwentyfive
twohundredandtwentysix
twohundredandtwentyseven
twohundredandtwentyeight
twohundredandtwentynine
twohundredandthirty
twohundredandthirtyone
twohundredandthirtytwo
twohundredandthirtythree
twohundredandthirtyfour
twohundredandthirtyfive
twohundredandthirtysix
twohundredandthirtyseven
twohundredandthirtyeight
twohundredandthirtynine
twohundredandfourty
twohundredandfourtyone
twohundredandfourtytwo
twohundredandfourtythree
twohundredandfourtyfour
twohundredandfourtyfive
twohundredandfourtysix
twohundredandfourtyseven
twohundredandfourtyeight
twohundredandfourtynine
twohundredandfifty
twohundredandfiftyone
twohundredandfiftytwo
twohundredandfiftythree
twohundredandfiftyfour
twohundredandfiftyfive
twohundredandfiftysix
twohundredandfiftyseven
twohundredandfiftyeight
twohundredandfiftynine
twohundredandsixty
twohundredandsixtyone
twohundredandsixtytwo
twohundredandsixtythree
twohundredandsixtyfour
twohundredandsixtyfive
twohundredandsixtysix
twohundredandsixtyseven
twohundredandsixtyeight
twohundredandsixtynine
twohundredandseventy
twohundredandseventyone
twohundredandseventytwo
twohundredandseventythree
twohundredandseventyfour
twohundredandseventyfive
twohundredandseventysix
twohundredandseventyseven
twohundredandseventyeight
twohundredandseventynine
twohundredandeighty
twohundredandeightyone
twohundredandeightytwo
twohundredandeightythree
twohundredandeightyfour
twohundredandeightyfive
twohundredandeightysix
twohundredandeightyseven
twohundredandeightyeight
twohundredandeightynine
twohundredandninety
twohundredandninetyone
twohundredandninetytwo
twohundredandninetythree
twohundredandninetyfour
twohundredandninetyfive
twohundredandninetysix
twohundredandninetyseven
twohundredandninetyeight
twohundredandninetynine
threehundred
threehundredandone
threehundredandtwo
threehundredandthree
threehundredandfour
threehundredandfive
threehundredandsix
threehundredandseven
threehundredandeight
threehundredandnine
threehundredandten
threehundredandeleven
threehundredandtwelve
threehundredandthirteen
threehundredandfourteen
threehundredandfifteen
threehundredandsixteen
threehundredandseventeen
threehundredandeighteen
threehundredandnineteen
threehundredandtwenty
threehundredandtwentyone
threehundredandtwentytwo
threehundredandtwentythree
threehundredandtwentyfour
threehundredandtwentyfive
threehundredandtwentysix
threehundredandtwentyseven
threehundredandtwentyeight
threehundredandtwentynine
threehundredandthirty
threehundredandthirtyone
threehundredandthirtytwo
threehundredandthirtythree
threehundredandthirtyfour
threehundredandthirtyfive
threehundredandthirtysix
threehundredandthirtyseven
threehundredandthirtyeight
threehundredandthirtynine
threehundredandfourty
threehundredandfourtyone
threehundredandfourtytwo
threehundredandfourtythree
threehundredandfourtyfour
threehundredandfourtyfive
threehundredandfourtysix
threehundredandfourtyseven
threehundredandfourtyeight
threehundredandfourtynine
threehundredandfifty
threehundredandfiftyone
threehundredandfiftytwo
threehundredandfiftythree
threehundredandfiftyfour
threehundredandfiftyfive
threehundredandfiftysix
threehundredandfiftyseven
threehundredandfiftyeight
threehundredandfiftynine
threehundredandsixty
threehundredandsixtyone
threehundredandsixtytwo
threehundredandsixtythree
threehundredandsixtyfour
threehundredandsixtyfive
threehundredandsixtysix
threehundredandsixtyseven
threehundredandsixtyeight
threehundredandsixtynine
threehundredandseventy
threehundredandseventyone
threehundredandseventytwo
threehundredandseventythree
threehundredandseventyfour
threehundredandseventyfive
threehundredandseventysix
threehundredandseventyseven
threehundredandseventyeight
threehundredandseventynine
threehundredandeighty
threehundredandeightyone
threehundredandeightytwo
threehundredandeightythree
threehundredandeightyfour
threehundredandeightyfive
threehundredandeightysix
threehundredandeightyseven
threehundredandeightyeight
threehundredandeightynine
threehundredandninety
threehundredandninetyone
threehundredandninetytwo
threehundredandninetythree
threehundredandninetyfour
threehundredandninetyfive
threehundredandninetysix
threehundredandninetyseven
threehundredandninetyeight
threehundredandninetynine
fourhundred
fourhundredandone
fourhundredandtwo
fourhundredandthree
fourhundredandfour
fourhundredandfive
fourhundredandsix
fourhundredandseven
fourhundredandeight
fourhundredandnine
fourhundredandten
fourhundredandeleven
fourhundredandtwelve
fourhundredandthirteen
fourhundredandfourteen
fourhundredandfifteen
fourhundredandsixteen
fourhundredandseventeen
fourhundredandeighteen
fourhundredandnineteen
fourhundredandtwenty
fourhundredandtwentyone
fourhundredandtwentytwo
fourhundredandtwentythree
fourhundredandtwentyfour
fourhundredandtwentyfive
fourhundredandtwentysix
fourhundredandtwentyseven
fourhundredandtwentyeight
fourhundredandtwentynine
fourhundredandthirty
fourhundredandthirtyone
fourhundredandthirtytwo
fourhundredandthirtythree
fourhundredandthirtyfour
fourhundredandthirtyfive
fourhundredandthirtysix
fourhundredandthirtyseven
fourhundredandthirtyeight
fourhundredandthirtynine
fourhundredandfourty
fourhundredandfourtyone
fourhundredandfourtytwo
fourhundredandfourtythree
fourhundredandfourtyfour
fourhundredandfourtyfive
fourhundredandfourtysix
fourhundredandfourtyseven
fourhundredandfourtyeight
fourhundredandfourtynine
fourhundredandfifty
fourhundredandfiftyone
fourhundredandfiftytwo
fourhundredandfiftythree
fourhundredandfiftyfour
fourhundredandfiftyfive
fourhundredandfiftysix
fourhundredandfiftyseven
fourhundredandfiftyeight
fourhundredandfiftynine
fourhundredandsixty
fourhundredandsixtyone
fourhundredandsixtytwo
fourhundredandsixtythree
fourhundredandsixtyfour
fourhundredandsixtyfive
fourhundredandsixtysix
fourhundredandsixtyseven
fourhundredandsixtyeight
fourhundredandsixtynine
fourhundredandseventy
fourhundredandseventyone
fourhundredandseventytwo
fourhundredandseventythree
fourhundredandseventyfour
fourhundredandseventyfive
fourhundredandseventysix
fourhundredandseventyseven
fourhundredandseventyeight
fourhundredandseventynine
fourhundredandeighty
fourhundredandeightyone
fourhundredandeightytwo
fourhundredandeightythree
fourhundredandeightyfour
fourhundredandeightyfive
fourhundredandeightysix
fourhundredandeightyseven
fourhundredandeightyeight
fourhundredandeightynine
fourhundredandninety
fourhundredandninetyone
fourhundredandninetytwo
fourhundredandninetythree
fourhundredandninetyfour
fourhundredandninetyfive
fourhundredandninetysix
fourhundredandninetyseven
fourhundredandninetyeight
fourhundredandninetynine
fivehundred
fivehundredandone
fivehundredandtwo
fivehundredandthree
fivehundredandfour
fivehundredandfive
fivehundredandsix
fivehundredandseven
fivehundredandeight
fivehundredandnine
fivehundredandten
fivehundredandeleven
fivehundredandtwelve
fivehundredandthirteen
fivehundredandfourteen
fivehundredandfifteen
fivehundredandsixteen
fivehundredandseventeen
fivehundredandeighteen
fivehundredandnineteen
fivehundredandtwenty
fivehundredandtwentyone
fivehundredandtwentytwo
fivehundredandtwentythree
fivehundredandtwentyfour
fivehundredandtwentyfive
fivehundredandtwentysix
fivehundredandtwentyseven
fivehundredandtwentyeight
fivehundredandtwentynine
fivehundredandthirty
fivehundredandthirtyone
fivehundredandthirtytwo
fivehundredandthirtythree
fivehundredandthirtyfour
fivehundredandthirtyfive
fivehundredandthirtysix
fivehundredandthirtyseven
fivehundredandthirtyeight
fivehundredandthirtynine
fivehundredandfourty
fivehundredandfourtyone
fivehundredandfourtytwo
fivehundredandfourtythree
fivehundredandfourtyfour
fivehundredandfourtyfive
fivehundredandfourtysix
fivehundredandfourtyseven
fivehundredandfourtyeight
fivehundredandfourtynine
fivehundredandfifty
fivehundredandfiftyone
fivehundredandfiftytwo
fivehundredandfiftythree
fivehundredandfiftyfour
fivehundredandfiftyfive
fivehundredandfiftysix
fivehundredandfiftyseven
fivehundredandfiftyeight
fivehundredandfiftynine
fivehundredandsixty
fivehundredandsixtyone
fivehundredandsixtytwo
fivehundredandsixtythree
fivehundredandsixtyfour
fivehundredandsixtyfive
fivehundredandsixtysix
fivehundredandsixtyseven
fivehundredandsixtyeight
fivehundredandsixtynine
fivehundredandseventy
fivehundredandseventyone
fivehundredandseventytwo
fivehundredandseventythree
fivehundredandseventyfour
fivehundredandseventyfive
fivehundredandseventysix
fivehundredandseventyseven
fivehundredandseventyeight
fivehundredandseventynine
fivehundredandeighty
fivehundredandeightyone
fivehundredandeightytwo
fivehundredandeightythree
fivehundredandeightyfour
fivehundredandeightyfive
fivehundredandeightysix
fivehundredandeightyseven
fivehundredandeightyeight
fivehundredandeightynine
fivehundredandninety
fivehundredandninetyone
fivehundredandninetytwo
fivehundredandninetythree
fivehundredandninetyfour
fivehundredandninetyfive
fivehundredandninetysix
fivehundredandninetyseven
fivehundredandninetyeight
fivehundredandninetynine
sixhundred
.....
ninehundredandninetyseven
ninehundredandninetyeight
ninehundredandninetynine
onethousand
看起来对我来说......
如果我使用for x in range(1, 6)
的示例对其进行测试,那就是19
如果我len(convert(115))
它会显示20
个字符,但如果我len(convert(342))
它会显示24
而不是23
...那么什么我做错了吗?
答案 0 :(得分:2)
看起来你需要在你的*字(二十,三十等)和一个数字(一,二等)之间划一条线。例如,我从描述中相信你应该得到“二十一”而不是“二十一”(顺便说一句,这不是一个词)。
您可能还想将“四十”改为“四十”。
答案 1 :(得分:1)
如前所述,40的单词是四十,而不是四十。
请注意,检查大约30个单独案例的一长串if
语句可以替换为数组引用。这将使程序缩短一半。
例如,使用以下内容替换teen
和placement
函数:
def teen(n):
n = int(n)
if n//10 == 1:
return ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"][n-10]
else:
return ""
def placement(n,p):
n = int(n)
if n < 1:
return ""
if p == 1:
return ["one","two","three","four","five","six","seven","eight","nine"][n-1]
if p == 2:
return ["ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"][n-1]
if p == 3:
return placement(n,1) + "hundred"
if p == 4:
return placement(n,1) + "thousand"
另外,将convert
的前6行替换为:
def convert(n):
if n < 10:
return placement(n,1)
if n < 20:
return teen(n)
if n < 100: