项目Euler 17使用Python

时间:2014-04-14 16:38:09

标签: python

我目前正在使用Python解决Project Euler的第17号问题。这是问题陈述的链接:

http://projecteuler.net/problem=17

这是我在Python中的解决方案:

def no_to_words(n):
    num = str(n)
    s = ""
    hunds = n/100
    ten = n%100
    tens = ten/10
    units = ten%10
    if(len(num) == 3):
        if(n == 100):
            return "one hundred"

        if(hunds == 1):                        
            s = s + "one hundred and"
        elif(hunds == 2):
            s = s + "two hundred and"
        elif(hunds == 3):
            s = s + "three hundred and"
        elif(hunds == 4):
            s = s + "four hundred and"
        elif(hunds == 5):
            s = s + "five hundred and"
        elif(hunds == 6):
            s = s + "six hundred and"
        elif(hunds == 7):
            s = s + "seven hundred and"
        elif(hunds == 8):
            s = s + "eight hundred and"
        else:
            s = s + "nine hundred and"

        if(ten == 11):                         
            s = s + " eleven"
            return s
        elif(ten == 12):
            s = s + " twelve"
            return s
        elif(ten == 13):
            s = s + " thirteen"
            return s
        elif(ten == 14):
            s = s + " fourteen"
            return s
        elif(ten == 15):
            s = s + " fifteen"
            return s
        elif(ten == 16):
            s = s + " sixteen"
            return s
        elif(ten == 17):
            s = s + " seventeen"
            return s
        elif(ten == 18):
            s = s + " eighteen"
            return s
        elif(ten == 19):
            s = s + " nineteen"
            return s

        if(tens == 2):
            s = s + " twenty"
        elif(tens == 3):
            s = s + " thirty"
        elif(tens == 4):
            s = s + " forty"
        elif(tens == 5):
            s = s + " fifty"
        elif(tens == 6):
            s = s + " sixty"
        elif(tens == 7):
            s = s + " seventy"
        elif(tens == 8):
            s = s + " eighty"
        elif(tens == 9):
            s = s + " ninety"

        if(units == 1):                         
            s = s + " one"
        elif(units == 2):
            s = s + " two"
        elif(units == 3):
            s = s + " three"
        elif(units == 4):
            s = s + " four"
        elif(units == 5):
            s = s + " five"
        elif(units == 6):
            s = s + " six"
        elif(units == 7):
            s = s + " seven"
        elif(units == 8):
            s = s + " eight"
        elif(units == 9):
            s = s + " nine"

    if(len(num) == 2):
        if(n == 10):
            return "ten"
        if(ten == 11):                         
            s = s + "eleven"
            return s
        elif(ten == 12):
            s = s + "twelve"
            return s
        elif(ten == 13):
            s = s + "thirteen"
            return s
        elif(ten == 14):
            s = s + "fourteen"
            return s
        elif(ten == 15):
            s = s + "fifteen"
            return s
        elif(ten == 16):
            s = s + "sixteen"
            return s
        elif(ten == 17):
            s = s + "seventeen"
            return s
        elif(ten == 18):
            s = s + "eighteen"
            return s
        elif(ten == 19):
            s = s + "nineteen"
            return s


        if(tens == 2):
            s = s + "twenty"
        elif(tens == 3):
            s = s + "thirty"
        elif(tens == 4):
            s = s + "forty"
        elif(tens == 5):
            s = s + "fifty"
        elif(tens == 6):
            s = s + "sixty"
        elif(tens == 7):
            s = s + "seventy"
        elif(tens == 8):
            s = s + "eighty"
        elif(tens == 9):
            s = s + "ninety"

        if(units == 1):
            s = s + " one"
        elif(units == 2):
            s = s + " two"
        elif(units == 3):
            s = s + " three"
        elif(units == 4):
            s = s + " four"
        elif(units == 5):
            s = s + " five"
        elif(units == 6):
            s = s + " six"
        elif(units == 7):
            s = s + " seven"
        elif(units == 8):
            s = s + " eight"
        elif(units == 9):
            s = s + " nine"

    if(len(num) == 1):
        if(units == 1):
            s = "one"
        elif(units == 2):
            s = "two"
        elif(units == 3):
            s = "three"
        elif(units == 4):
            s = "four"
        elif(units == 5):
            s = "five"
        elif(units == 6):
            s = "six"
        elif(units == 7):
            s = "seven"
        elif(units == 8):
            s = "eight"
        elif(units == 9):
            s = "nine"

    if(len(num) == 4):
        return "one thousand"

    return s

final = ""
for e in range(1, 1001):
    s = no_to_words(e)
    final = final + s
ultimate = final.replace(" ", "")
print len(ultimate)

现在,这个问题的答案是(剧透):

  

21124

然而,我得到了21121作为我的答案。我的代码出了什么问题?我似乎无法弄清楚问题。

2 个答案:

答案 0 :(得分:3)

您从未涉及tens==1的情况。而且你不会考虑100的精确倍数。

答案 1 :(得分:1)

看起来你可能会在这里重新发明一下这个轮子。我没有使用no_to_words,而是使用了num2words库。这似乎给了我正确的答案。

total = 0
for num in range(1, 1001):
    total += len(num2words(num).replace(" ", "").replace("-", ""))

print(total)
21124