0

可能重复:
Project Euler 17

所以对于项目euler的这个问题:

如果数字 1 到 5 用单词写出:一、二、三、四、五,那么总共使用了 3 + 3 + 5 + 4 + 4 = 19 个字母。

如果用文字写出从 1 到 1000(一千)的所有数字,将使用多少个字母?

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

我试图在没有帮助的情况下自己解决这个问题......但我认为我做错了什么......

我有这个代码...

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......那么我做错了什么?

4

2 回答 2

2

看起来您需要在您的 *ty 单词(二十、三十等)和一个数字(一、二等)之间添加一个破折号。例如,我相信从描述中你应该得到“二十一”而不是“二十一”(顺便说一句,这不是一个词)。

您可能还想将“四十”更改为“四十”。

于 2012-10-02T23:54:58.363 回答
1

如前所述, 40 一词是40,而不是40

请注意,if检查大约 30 个单独案例的长系列语句可以替换为数组引用。这将使程序减半。

例如,将您的teenplacement功能替换为以下内容:

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"

此外,将前 6 行替换为convert

def convert(n):
    if n < 10:
        return placement(n,1)
    if n < 20:
        return teen(n)
    if n < 100:
于 2012-10-03T03:25:13.347 回答