题解 | #学英语#

学英语

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

  # 笨比做法
def numToEng(n):
    lst1 = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ']
    lst2 = ['', 'ten', 'twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ']
    lst3 = ['', 'eleven', 'twelve', 'thirteen', 'forteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
    result = ''
    l = len(n)
    if l == 3:
        if n[1] == '0' and n[2] == '0':
            result += lst1[int(n[0])] + 'hundred'
        else:
            if n[1] == '1' and n[2] == '0':
                result += lst1[int(n[0])] + 'hundred and ten'
            elif n[1] == '1' and n[2] != '0':
                result += lst1[int(n[0])] + 'hundred and ' + lst3[int(n[2])]
            else:
                result = result + lst1[int(n[0])] + 'hundred and ' + lst2[int(n[1])] + lst1[int(n[2])]
    elif l == 2:
        if n[0] == '1' and n[1] == '0':
            result += 'ten'
        elif n[0] == '1' and n[1] != '0':
            result += lst3[int(n[2])]
        else:
            result += lst2[int(n[0])] + lst1[int(n[1])]
    elif l == 1:
        result += lst1[int(n)]
    return result


s = format(int(input()), ',').split(',')

engNum = ''
n = []
for i in s:
    n.append(i.lstrip('0'))
l = len(n)
if l == 4:
    engNum += numToEng(n[0]) + 'billion ' + numToEng(n[1]) + 'million ' + numToEng(n[2]) + 'thousand ' + numToEng(
        n[3])
elif l == 3:
    engNum += numToEng(n[0]) + 'million ' + numToEng(n[1]) + 'thousand ' + numToEng(n[2])
elif l == 2:
    engNum += numToEng(n[0]) + 'thousand ' + numToEng(n[1])
elif l == 1:
    engNum += numToEng(n[0])
print(engNum)

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:25
点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
07-03 11:02
中山大学 C++
字节刚oc,但距离九月秋招很近了有两段互联网实习,非腾讯字节。不敢赌转正,现在在纠结去还是不去如果实习俩月离职会有什么后果吗
阿城我会做到的:不去后悔一辈子,能否转正取决于ld的态度,只要他不卡,答辩就是走流程,个人觉得可以冲一把
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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