|  | 
 
30鱼币 
| 本帖最后由 zltzlt 于 2020-2-14 17:51 编辑 
 
 今天的题目: 
 给定一个非负整数(小于 2147483648),返回这个非负整数对应的英文表示(每个单词首字母大写)。
 
 
 示例 1:
 输入:123
 输出:"One Hundred Twenty Three"
示例 2:
 输入:12345
 输出:"Twelve Thousand Three Hundred Forty Five"
示例 3:
 输入:1234567
 输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
示例 4:
 输入:1234567891
 输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
  欢迎大家一起答题! 
 本帖最后由 William4869 于 2020-2-14 19:27 编辑 
复制代码def f332(x):
    dict1={'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','100':'Hundred'}
    s=""
    if x==0:
        s+="Zero"
        return s
    if x>999999999:
        s+=dict1[str(x//1000000000)]
        s+=" Billion"
        x%=1000000000
    if x>999999:
        t=x//1000000
        if t>99:
            if s!="":
                s+=" "
            s+=dict1[str(t//100)]
            s+=" Hundred"
            t%=100
        if t!=0:
            if t<21 or t%10==0:
                if s!="":
                    s+=" "
                s+=dict1[str(t%100)]
                s+=" Million"
            else:
                if s!="":
                    s+=" "
                s+=dict1[str(t%100//10*10)]
                s+=" "
                s+=dict1[str(t%10)]
                s+=" Million"
        else:
            s+=" Million"
        x%=1000000
    if x>999:
        t=x//1000
        if t>99:
            if s!="":
                s+=" "
            s+=dict1[str(t//100)]
            s+=" Hundred"
            t=t%100
        if t!=0:
            if t<21 or t%10==0:
                if s!="":
                    s+=" "
                s+=dict1[str(t%100)]
                s+=" Thousand"
            else:
                if s!="":
                    s+=" "
                s+=dict1[str(t%100//10*10)]
                s+=" "
                s+=dict1[str(t%10)]
                s+=" Thousand"
        else:
            s+=" Thousand"
        x%=1000
 
    t=x
    if t>99:
        if s!="":
            s+=" "
        s+=dict1[str(t//100)]
        s+=" Hundred"
        t=t%100
    if t==0:return s
    if t<21 or t%10==0:
        if s!="":
            s+=" "
        s+=dict1[str(t%100)]
    else:
        if s!="":
            s+=" "
        s+=dict1[str(t%100//10*10)]
        s+=" "
        s+=dict1[str(t%10)]
    return s
print(f332(1400000))
无脑码代码,来看看我有没有遗漏什么 | 
 |