题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

n = "{:,}".format(int(input()))
d = {
    "0": "",
    "1": "one",
    "2": "two",
    "3": "three",
    "4": "four",
    "5": "five",
    "6": "six",
    "7": "seven",
    "8": "eight",
    "9": "nine",
    "10":"ten",
    "11":"eleven",
    "12":"twelve",
    "13":"thirteen",
    "14":"fourteen",
    "15":"fifteen",
    "16":"sixteen",
    "17":"seventeen",
    "18":"eighteen",
    "19":"nineteen",
    0:"",
    1: "ten",
    2: "twenty",
    3: "thirty",
    4: "forty",
    5: "fifty",
    6: "sixty",
    7: "seventy",
    8: "eighty",
    9: "ninety",
}

# 因为n的取值范围在[1,2000000]之间,所以len的值最多为3
lt = n.split(",")
len = len(lt)
res = []
if len == 3:
    res.append(d[lt[0]])
    res.append("million")
    if int(lt[1]) == 0:
        pass
    elif int(lt[1]) <= 9:
        res.append(d[lt[1][2]])
        res.append("thousand")
    elif int(lt[1]) <= 99:
        if int(lt[1])%10 == 0:
            res.append(d[int(lt[1][1])])
            res.append("thousand")
        elif int(lt[1]) <= 19:
            res.append(d[lt[1][1:]])
        else:
            res.append(d[int(lt[1][1])])
            res.append(d[lt[1][2]])
            res.append("thousand")
    else:
        if int(lt[1]) % 100 == 0:
            res.append(d[lt[1][0]])
            res.append("thousand")
        elif int(lt[1]) % 10 == 0:
            res.append(d[lt[1][0]])
            res.append("hundred")
            res.append("and")
            res.append(d[int(lt[1][1])])
            res.append("thousand")
        else:
            res.append(d[lt[1][0]])
            res.append("hundred")
            res.append("and")
            if d[int(lt[1][1])] != '':
                res.append(d[int(lt[1][1])])
            res.append(d[lt[1][2]])
            res.append("thousand")
    if int(lt[2]) == 0:
        pass
    elif int(lt[2]) <= 9:
        res.append(d[lt[2][2]])
    elif int(lt[2]) <= 99:
        if int(lt[2]) % 10 == 0:
            res.append(d[int(lt[2][1])])
        else:
            res.append(d[int(lt[2][1])])
            res.append(d[lt[2][2]])
    else:
        if int(lt[2]) % 100 == 0:
            res.append(d[lt[2][0]])
            res.append("hundred")
        elif int(lt[2]) % 10 == 0:
            res.append(d[lt[2][0]])
            res.append("hundred")
            res.append("and")
            res.append(d[int(lt[2][1])])
        else:
            res.append(d[lt[2][0]])
            res.append("hundred")
            res.append("and")
            if lt[2][1] != "1":
                if d[int(lt[2][1])] != '':
                    res.append(d[int(lt[2][1])])
                res.append(d[lt[2][2]])
            else:
                res.append(d[lt[2][1:]])
    print(" ".join(res))
elif len == 2:
    if int(lt[0]) <= 9:
        res.append(d[lt[0]])
        res.append("thousand")
    elif int(lt[0]) <= 99:
        if int(lt[0]) % 10 == 0:
            res.append(d[int(lt[0][0])])
            res.append("thousand")
        else:
            res.append(d[int(lt[0][0])])
            res.append(d[lt[0][1]])
            res.append("thousand")
    else:
        if int(lt[0]) % 100 == 0:
            res.append(d[lt[0][0]])
            res.append("hundred")
            res.append("thousand")
        elif int(lt[0]) % 10 == 0:
            res.append(d[lt[0][0]])
            res.append("hundred")
            res.append("and")
            res.append(d[int(lt[0][1])])
            res.append("thousand")
        else:
            res.append(d[lt[0][0]])
            res.append("hundred")
            res.append("and")
            if d[int(lt[0][1])] != '':
                res.append(d[int(lt[0][1])])
            res.append(d[lt[0][2]])
            res.append("thousand")
    if int(lt[1]) == 0:
        pass
    elif int(lt[1]) <= 9:
        res.append(d[lt[1][2]])
    elif int(lt[1]) <= 99:
        if int(lt[1]) % 10 == 0:
            res.append(d[int(lt[1][1])])
        else:
            res.append(d[int(lt[1][1])])
            res.append(d[lt[1][2]])
    else:
        if int(lt[1]) % 100 == 0:
            res.append(d[lt[1][0]])
            res.append("hundred")
        elif int(lt[1]) % 10 == 0:
            res.append(d[lt[1][0]])
            res.append("hundred")
            res.append("and")
            res.append(d[int(lt[1][1])])
        else:
            res.append(d[lt[1][0]])
            res.append("hundred")
            res.append("and")
            if lt[1][1] != "1":
                if lt[1][1] != "0":
                    res.append(d[int(lt[1][1])])
                res.append(d[lt[1][2]])
            else:
                res.append(d[lt[1][1:]])
    print(" ".join(res))
elif len == 1:
    if int(lt[0]) <= 9:
        res.append(d[lt[0][0]])
    elif int(lt[0]) <= 99:
        if int(lt[0]) % 10 == 0:
            res.append(d[int(lt[0][0])])
        elif int(lt[0]) <= 19:
            res.append(d[lt[0]])
        else:
            res.append(d[int(lt[0][0])])
            res.append(d[lt[0][1]])
    else:
        if int(lt[0]) % 100 == 0:
            res.append(d[lt[0][0]])
            res.append("hundred")
        elif int(lt[0]) % 10 == 0:
            res.append(d[lt[0][0]])
            res.append("hundred")
            res.append("and")
            res.append(d[int(lt[0][1])])
        else:
            res.append(d[lt[0][0]])
            res.append("hundred")
            res.append("and")
            if lt[0][1] != '1':
                if d[int(lt[0][1])] != '':
                    res.append(d[int(lt[0][1])])
                res.append(d[lt[0][2]])
            else:
                res.append(d[lt[0][1:]])
    print(' '.join(res))

全部评论

相关推荐

09-24 18:30
已编辑
长春工业大学 产品经理
小肥罗:HR就是好人的缩写哈哈哈哈
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务