题解 | #学英语#

学英语

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

讲道理,我觉得我这个方法很拉胯

def get_twenty(x):
    if x % 10 == 0 and x != 10:
        return dict_2[str(x)[0]]
    elif x > 20:
        y = str(x)[0]
        z = str(x)[1:]
        return dict_2[y] + ' ' + dict_1[str(int(z))]
    return dict_1[str(x)]


def get_hundred(x):
    if x <= 20:
        return get_twenty(x)
    y = str(x)[0]
    z = str(x)[1:]
    if x % 10 == 0 and x < 100:
        return dict_2[str(x)[0]]
    elif x % 100 == 0 and x != 100:
        return f'{get_twenty(y)} hundred'
    elif x == 100:
        return 'one hundred'
    elif x == 1000:
        return 'one thousand'
    elif x > 100:
        return dict_1[y] + ' hundred and ' + get_twenty(int(z))
    elif 20 < x < 100:
        return dict_2[y] + ' ' + get_twenty(int(z))
    else:
        return dict_1[y] + get_twenty(int(z))


def get_thousand(x):
    y = str(x)[0:-3]
    z = str(x)[-3:]
    if x % 1000 == 0:
        return f'{get_twenty(y)} thousand'
    elif x == 10000:
        return 'ten thousand'
    else:
        return get_hundred(int(y)) + ' thousand ' + get_hundred(int(z))


def get_million(x):
    y = str(x)[0:-6]
    z = str(x)[-6:]
    if x % 1000000 == 0:
        return f'{get_twenty(y)} million'
    else:
        return get_hundred(int(y)) + ' million ' + get_thousand(int(z))


while True:
    try:
        input_res = int(input())
        dict_1 = {
            '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',
            '20': 'twenty',
        }
        dict_2 = {
            '2': 'twenty',
            '3': 'thirty',
            '4': 'forty',
            '5': 'fifty',
            '6': 'sixty',
            '7': 'seventy',
            '8': 'eighty',
            '9': 'ninety',
        }
        if input_res <= 20:
            res = get_twenty(input_res)
        elif 20 < input_res <= 100:
            res = get_hundred(input_res)
        elif 100 < input_res <= 1000:
            res = get_hundred(input_res)
        elif 1000 < input_res < 1000000:
            res = get_thousand(input_res)
        elif 1000000 <= input_res:
            res = get_million(input_res)
        else:
            res = ''
        print(res)
    except EOFError:
        break



全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务