2019微软summer intern面试题记录

有一阿拉伯字符串,判断是否合法,将其译为中文。

如:1234   >>>>>   一千二百三十四;

不合法字符串举例:0123;

字符串长度在12个字符以内。

(苏州,2019.4.26)

解答:

def read(s):
    if s == "0":
        ans = '零'
    elif s == '1':
        ans = '一'
    elif s == '2':
        ans = '二'
    elif s == '3':
        ans = '三'
    elif  == '4':
        ans = '四'
    elif s == '5':
        ans = '5'
    elif s == '6':
        ans = '六'
    elif s == '7':
        ans = '七'
    elif s == '8':
        ans = '八'
    elif s == '9':
        ans = '九'
    return ans
def convert(s):
    n = len(s)
    if n == 1:
        ans = read(s)
        
    elif n == 2:
        if s[1] == '0' and s[0] != '0':
            ans0 = read(s[0])
            ans1 = ''
            ans = ans0 + '十'
        elif s[1] != '0' and s[0] != '0':
            ans0 = read(s[0])
            ans1 = read(s[1])
            ans = ans0 + '十' + ans1
        elif s[1] == '0'and s[0] == '0':
            ans = ''
        elif s[1] != '0'and s[0] == '0':
            ans0 = '零'
            ans1 = read(s[1])
            ans = ans0 + ans1

    elif n == 3:
        if s[0] != '0':
            ans0 = read(s[0])
            ans1 = convert(s[1:3])
            ans = ans0 + '百' + ans1
        elif s[0] == '0':
            if s[1:3] == '00':
                ans = ''
            elif s[1] == '0' and s[2] != '0':
                ans0 = '零'
                ans1 = ''
                ans2 = read(s[2])
                ans = ans0 + ans1 + ans2
            else:
                ans0 = '零'
                ans1 = convert(s[1:3])
                ans = ans0 + ans1
        
    elif n == 4:
        if s[0] != '0':
            ans0 = read(s[0])
            ans1 = convert(s[1:4])
            ans = ans0 + '千' + ans1
        elif s[0] == '0':
            if s[1:4] == '000':
                ans = ''
            elif s[1] == '0':
                ans0 = ''
                ans1 = convert(s[1:4])
                ans = ans0 + ans1
            elif s[1] != '0':
                ans0 = '零'
                ans1 = convert(s[1:4])
                ans = ans0 + ans1
    return ans
import math
def split(s):
    n = len(s)
    num = math.ceil(n/4)
    string = []
    i = -1
    stemp = ''
    while abs(i) <= n:
        stemp = s[i] + stemp
        if len(stemp) == 4:
            string.insert(0, stemp)
            stemp = ''
        i -= 1
    string.insert(0, stemp)
    return string
def solution(s):
    if s == '':
        print('空字符串')
    else:
        if s[0] == '0':
            print('不合法')
        else:
            string = split(s)
            n = len(string)
            if n == 1:
                ans = convert(string[0])
            elif n == 2:
                ans0 = convert(string[0])
                ans1 = convert(string[1])
                ans = ans0 + '万' + ans1
            elif n == 3:
                ans0 = convert(string[0])
                ans1 = convert(string[1])
                ans2 = convert(string[2])
                ans = ans0 + '亿' + ans1 + '万' + ans2
            print(ans)
ss = '1111111111'
solution(ss)

答案为:

一十一亿一千一百一十一万一千一百一十一
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书生伯言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值