欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2019微软summer intern面试题记录

程序员文章站 2024-01-10 10:03:40
...

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

如: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)

答案为:

一十一亿一千一百一十一万一千一百一十一