题解 | #学英语#

学英语

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)

全部评论

相关推荐

永不遗忘:才这么点算什么拉黑,我初筛连着挂几十次了,最后还是能进面
点赞 评论 收藏
分享
03-30 19:30
石家庄学院 Java
野蛮的柯基在游泳:都能入股了,还得是Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务