The number of basic exercises Python Blue Bridge cup readings

The number of basic questions to practice pronunciation

Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem Description
  Professor Tom is to teach a graduate course on genes, one thing made him quite a headache: there are tens of thousands of base pairs on a chromosome, they numbered from 0 to millions, tens of millions or even billions.
  For example, when the students about the bases at position No. 1234567009, the light looks figures are difficult to accurately read out.
  So, he desperately needs a system, and then when he entered 1,234,567,009 time, will give the corresponding read the law:
  1,234,567,009
  represented as Pinyin
  shi er yi san qian si bai wu shi liu wan qi qian ling jiu
  so he only needs to read shining on it.
  Your task is to help him design a system like this: Given a string of digits, you help him into reading and writing Chinese Pinyin string accordance with the norms of the adjacent two syllables with a lattice open spaces.
  Note must be in strict accordance with specifications, such as "10010" is read as "yi wan ling yi shi" instead of "yi wan ling shi", " 100000" is read as "shi wan" instead of "yi shi wan", "2000 " read as "er qian" instead of "liang qian".
Input format
  has a sequence of digits, the numerical size of not more than 2,000,000,000.
Output format
  is a string of lower case letters, spaces and commas composition, represents the number of English reading.
Sample Input
1234567009
Sample Output
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
ideas
from bits to one thousand, million were from every four to ten million ...... Read method is the same, but in the back add a ' wan 'or' yi '. Therefore, the entire string backwards into a list of every four time taken, write a function of a digit read method. The list of elements in turn call reading function, according to the bit right before adding "wan" or "yi".

d = {'1':'yi', '2':'er','3':'san','4':'si','5':'wu','6':'liu','7':'qi','8':'ba','9':'jiu','0':'ling'}
n = input().strip()
a = []
b = [] #b存的是逆序的读法,最后再逆序输出即为结果
def dushu(i): 
    i = i[::-1]
    for j in range(len(i)):
        if j == 0:
            if i[j]!='0':
                b.append(d[i[j]])
        elif j == 1:
            if i[j]=='0'  and  i[0]!='0':
                b.append(d['0'])
            elif i[j] == '1' and ( len(i)==2 or i[2] == i[3] == '0'):
                b.append('shi')
            elif i[j]!='0':
                b.append('shi')
                b.append(d[i[j]])
        elif j == 2:
            if i[j]=='0'  and i[1]!='0':
                b.append(d['0'])
            elif i[j]!='0':
                b.append('bai')
                b.append(d[i[j]])
        elif j == 3:
            if  i[j]!='0':
                b.append('qian')
                b.append(d[i[j]])
            elif i[j]=='0'  and i[2]!='0':
                b.append(d['0'])

while n!='':  #倒着读,每四位为一组字符串
    if len(n)>=4:
        s = n[-4:] #取后四位
        n = n[:-4] #删除后四位
        a.append(s) 
    else:
        a.append(n)
        n = ''
for i in range(len(a)):
    if i == 0:
        dushu(a[i])
    elif i == 1:
        b.append('wan')
        dushu(a[i])
    elif i == 2:
        b.append('yi')
        dushu(a[i])
for i in range(len(b)-1,-1,-1):
    if i == len(b)-1:
        print(b[i],end='')
    else:
        print(' %s'%(b[i]),end='')
Released five original articles · won praise 0 · Views 235

Guess you like

Origin blog.csdn.net/qq_29922685/article/details/104887547