题解 | #学英语#

学英语

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

#先建立一个字典把对应数字的英文存起来
#在写个获取个位的,十位的,百位的表示
#千和百万,十亿的三位可以共用获得一百内的表示
import sys
word = {'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",'30':"thirty",'40':"forty",'50':"fifty",'60':"sixty",'70':"seventy",'80':"eighty",'90':"ninety"}
def get_billion(num):
    return word[str(num)] + " billion"

def get_million(num):
    return get_hundred(num // 1000000) + " million"

def get_thousand(num):
    return get_hundred(num // 1000) + " thousand"

def get_hundred(num):
    re = ''
    if num // 100 > 0:
        re += word[str(num // 100)] + " hundred"
        if num % 100 != 0:
            re += ' and '
    re += get_shi(num % 100)
    return re

def get_shi(num):
    if num < 20:
        return word[str(num)]
    else:
        return word[str(num // 10 * 10)] + get_ge(num % 10)

def get_ge(num):
    if num == 0:
        return ""
    return " " + word[str(num)]

while True:
    try:
        num = int(input())
        result = ""
        if num // 1000000000 > 0:
            result += get_billion(num // 1000000000)
            num = num % 1000000000
        if num // 1000000 > 0:
            if result != "":
                result += " "
            result += get_million(num)
            num = num % 1000000
        if num // 1000 > 0:
            if result != "":
                result += " "
            result += get_thousand(num)
            num = num % 1000
        if num % 100 > 0:    
            if result != "":
                result += " "
            result += get_hundred(num)
        print(result)
    except:
#         print(sys.exc_info())
        break









全部评论

相关推荐

10-17 12:16
同济大学 Java
7182oat:快快放弃了然后发给我,然后让我也泡他七天最后再拒掉,狠狠羞辱他一把😋
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务