题解 | #学英语#

学英语

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

num1 = ['zero', 'one', 'two', 'three', 'four', 'five', 'six',
        'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
        'thirteen', 'fourteen', 'fifteen', 'sixteen',
        'seventeen', 'eighteen', 'nineteen']
num2 = [0, 0, 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
        'seventy', 'eighty', 'ninety']


def read2(n):
    """转换100以内的数"""
    res2 = []
    if n != 0:
        if n < 20:
            res2.append(num1[n])
        else:
            res2.append(num2[n // 10])
            if n % 10 != 0:
                res2.append(num1[n % 10])
    return res2


def read3(n):
    """转换1000以内的数"""
    res3 = []
    if n >= 100:
        res3 = [num1[n // 100], 'hundred']
        if n % 100 != 0:
            res3.append('and')
    res3 += read2(n % 100)
    return res3


while True:
    try:
        num = input()[::-1]
        res = ''
        lst = []
        for i in range(0, len(num), 3):
            lst.append(num[i:i + 3][::-1])
        num_lst = list(map(int, lst))
        if len(num_lst) == 1:
            res += ' '.join(str(word) for word in read3(num_lst[0]))
            print(res)
        elif len(num_lst) == 2:
            res += ' '.join(str(word) for word in read3(num_lst[1]))
            res += ' thousand '
            res += ' '.join(str(word) for word in read3(num_lst[0]))
            print(res)
        elif len(num_lst) == 3:
            res += ' '.join(str(word) for word in read3(num_lst[2]))
            res += ' million '
            res += ' '.join(str(word) for word in read3(num_lst[1]))
            res += ' thousand '
            res += ' '.join(str(word) for word in read3(num_lst[0]))
            print(res)
        elif len(num_lst) == 4:
            res += ' '.join(str(word) for word in read3(num_lst[3]))
            res += ' billion '
            res += ' '.join(str(word) for word in read3(num_lst[2]))
            res += ' million '
            res += ' '.join(str(word) for word in read3(num_lst[1]))
            res += ' thousand '
            res += ' '.join(str(word) for word in read3(num_lst[0]))
            print(res)
    except:
        break
全部评论

相关推荐

点赞 评论 收藏
分享
门口唉提是地铁杀:之前b站被一个游戏demo深深的吸引了。看up主页发现是个初创公司,而且还在招人,也是一天60。二面的时候要我做一个登录验证和传输文件两个微服务,做完要我推到github仓库,还要我加上jaeger和一堆运维工具做性能测试并且面试的时候投屏演示。我傻乎乎的做完以后人家跟我说一句现在暂时不招人,1分钱没拿到全是白干
你的秋招第一场笔试是哪家
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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