#define function读每四位数
#注意:每四位数中间有0的情况和每四位数末尾有0的情况
def dushu(str):
temp=[]
haszero=0
for i in range(len(str)):
if str[i]=='0':
if haszero==0:
haszero=1
temp.append(dict[str[i]])
continue
else:
continue
else:
haszero=0
temp.append(dict[str[i]])
temp.append(wei[len(str) - 1 - i])
temp=' '.join(temp).strip()#返回以空格为分隔符的字符串
while temp.endswith('ling'):#去掉末尾的零
temp=temp.rstrip('ling')
temp=temp.strip()
return temp
n=input()
dict={'0':'ling','1':'yi','2':'er','3':'san','4':'si','5':'wu',
'6':'liu','7':'qi','8':'ba','9':'jiu'}
wei=['','shi','bai','qian']
dawei=['','wan','yi']
ls=[]#存储四个一组划分之后的数组(为字符串)
ans=[]#储存答案
#将数字四个四个划为一组
while n!='': #倒着读,每四位为一组字符串
if len(n)>=4:
s = n[-4:] #取后四位
n = n[:-4] #删除后四位
ls.append(s)
else:
ls.append(n)
n = ''
#ls[0]为最低位的四位数
##合并答案
##注意:如果四位全部为0,则不要加dawei(亿,万)
for i in range(len(ls)):
if int(ls[i])==0:
continue
else:
temp=dushu(ls[i])+' '+dawei[i]
ans.insert(0,temp)
ans=' '.join(ans).strip()
##检查开头是否为一十
if ans.startswith('yi shi'):
ans=ans.lstrip('yi')
ans=ans.strip()
print(ans)
Python蓝桥杯 ---- 数的读法
于 2023-04-01 15:36:55 首次发布