题解 | #学英语#

学英语

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

import sys

table1 = {
    "0": "",
    "1": "one",
    "2": "two",
    "3": "three",
    "4": "four",
    "5": "five",
    "6": "six",
    "7": "seven",
    "8": "eight",
    "9": "nine",
    "00": "",
    "10": "ten",
    "11": "eleven",
    "12": "twelve",
    "13": "thirteen",
    "14": "fourteen",
    "15": "fifteen",
    "16": "sixteen",
    "17": "seventeen",
    "18": "eighteen",
    "19": "nineteen",
    "20": "twenty",
    "30": "thirty",
    "40": "forty",
    "50": "fifty",
    "60": "sixty",
    "70": "seventy",
    "80": "eighty",
    "90": "ninety",
}


def cat1(a):
    list1 = []
    if len(a) <= 3:
        return 0, [a]
    if len(a) % 3 != 0:
        list1.append(a[0 : len(a) % 3])
        a = a[len(a) % 3 :]
    for i in [a[i : i + 3] for i in range(0, len(a), 3)]:
        if i:
            list1.append(i)
    return len(list1), list1


resault2 = []
for line in sys.stdin:
    count1, resault1 = cat1(line.strip())
    for i in resault1:
        finally1 = ""
        just1 = len(i)
        if just1 == 3:
            if i[0] != "0":
                if i[1] == i[2] == "0":
                    finally1 += table1[i[0]] + " hundred"
                elif i[1] == "0":
                    finally1 += table1[i[0]] + " hundred and " + table1[str(i[2])]
                elif i[1] == "1" or i[2] == "0":
                    finally1 += table1[i[0]] + " hundred and " + table1[i[1] + i[2]]
                elif i[1] != "1":
                    finally1 += (
                        table1[i[0]]
                        + " hundred and "
                        + table1[i[1] + "0"]
                        + " "
                        + table1[str(i[2])]
                    )
            else:
                i = i[1:]
                just1 = 2
        if just1 == 2:
            if i[0] != "0":
                if i[0] == "1" or i[1] == "0":
                    finally1 += table1[str(i[0]) + i[1]]
                elif i[1] != "1":
                    finally1 += table1[str(i[0]) + "0"] + " " + table1[str(i[1])]
            else:
                i = i[1:]
                just1 = 1
        if just1 == 1:
            finally1 += table1[str(i[0])]
        resault2.append([finally1, count1])
        count1 -= 1
    finally1 = ""
    for i in resault2:
        if int(i[1]) == 2:
            finally1 += i[0] + " thousand "
        elif int(i[1]) == 3:
            finally1 += i[0] + " million "
        else:
            finally1 += i[0]

    print(finally1)

全部评论

相关推荐

09-26 19:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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