原题链接:蓝桥杯基础练习VIP-数的读法
解题思路: 首先判断输入的串的位数,根据位数分为三类,第一类是大于8位,第二类是大于4位并且小于9位,第三类是小于5位,读的时候分为三个级别,个级、万级、亿级,分别写三个函数。
注意事项:这里说一下第一个难点是每个级别的两位数的读法,分为四种情况,这里以个级别为例,
if ge[0]==1: #高位为1的情况,读的时候应该读shi
if ge[1]==0: #低位的0不读
return(n_2[0])
else: #其他情况
return(n_2[0]+' '+n_1[ge[1]])
else: #其他情况
if ge[1]==0: #低位的0不读
return(n_1[ge[0]]+' '+n_2[0])
else: #其他情况
return(n_1[ge[0]]+' '+n_2[0]+' '+n_1[ge[1]])
参考代码:
#试题 基础练习 数的读法 n_1=['ling','yi','er','san','si','wu','liu','qi','ba','jiu'] n_2=['shi','bai','qian','wan','yi'] def reader_ge(ge): ge=list(map(int,ge)) if len(ge) == 4: if ge[0]==0: if ge[1]==0 and ge[2]==0 and ge[3]==0: return('') elif ge[1]==0 and ge[2]==0 and ge[3]!=0: return(n_1[ge[1]]+' '+n_1[ge[3]]) elif ge[1]==0 and ge[2]!=0 and ge[3]==0: return(n_1[ge[1]]+' '+n_1[ge[2]]+' '+n_2[0]) elif ge[1]==0 and ge[2]!=0 and ge[3]!=0: return(n_1[ge[1]]+' '+n_1[ge[2]]+' '+n_2[0]+' '+n_1[ge[3]]) elif ge[1]!=0 and ge[2]==0 and ge[3]==0: return(n_1[ge[0]]+' '+n_1[ge[1]]+' '+n_2[1]) elif ge[1]!=0 and ge[2]==0 and ge[3]!=0: return(n_1[ge[0]]+' '+n_1[ge[1]]+' '+n_2[1]+' '+n_1[ge[2]]+' '+n_1[ge[3]]) elif ge[1]!=0 and ge[2]!=0 and ge[3]==0: return(n_1[ge[0]]+' '+n_1[ge[1]]+' '+n_2[1]+' '+n_1[ge[2]]+' '+n_2[0]) else: return(n_1[ge[0]]+' '+n_1[ge[1]]+' '+n_2[1]+' '+n_1[ge[2]]+' '+n_2[0]+' '+n_1[ge[3]]) else: if ge[1]==0 and ge[2]==0 and ge[3]==0: return(n_1[ge[0]]+' '+n_2[2]) elif ge[1]==0 and ge[2]==0 and ge[3]!=0: return(n_1[ge[0]]+' '+n_2[2]+' '+n_1[ge[1]]+' '+n_1[ge[3]]) elif ge[1]==0 and ge[2]!=0 and ge[3]==0: return(n_1[ge[0]]+' '+n_2[2]+' '+n_1[ge[1]]+' '+n_1[ge[2]]+' '+n_2[0]) elif ge[1]==0 and ge[2]!=0 and ge[3]!=0: return(n_1[ge[0]]+' '+n_2[2]+' '+n_1[ge[1]]+' '+n_1[ge[2]]+' '+n_2[0]+' '+n_1[ge[3]]) elif ge[1]!=0 and ge[2]==0 and ge[3]==0: return(n_1[ge[0]]+' '+n_2[2]+' '+n_1[ge[1]]+' '+n_2[1]) elif ge[1]!=0 and ge[2]==0 and ge[3]!=0: return(n_1[ge[0]]+' '+n_2[2]+' '+n_1[ge[1]]+' '+n_2[1]+' '+n_1[ge[2]]+' '+n_1[ge[3]]) elif ge[1]!=0 and ge[2]!=0 and ge[3]==0: return(n_1[ge[0]]+' '+n_2[2]+' '+n_1[ge[1]]+' '+n_2[1]+' '+n_1[ge[2]]+' '+n_2[0]) else: return(n_1[ge[0]]+' '+n_2[2]+' '+n_1[ge[1]]+' '+n_2[1]+' '+n_1[ge[2]]+' '+n_2[0]+' '+n_1[ge[3]]) elif len(ge) == 3: if ge[1] == 0 and ge[2] == 0: return(n_1[ge[0]]+' '+n_2[1]) elif ge[1] == 0 and ge[2] !=0: return(n_1[ge[0]]+' '+n_2[1]+' '+n_1[ge[1]]+' '+n_1[ge[2]]) elif ge[1]!=0 and ge[2]==0: return(n_1[ge[0]]+' '+n_2[1]+' '+n_1[ge[1]]+' '+n_2[0]) else: return(n_1[ge[0]]+' '+n_2[1]+' '+n_1[ge[1]]+' '+n_2[0]+' '+n_1[ge[2]]) elif len(ge) == 2: if ge[0]==1: if ge[1]==0: return(n_2[0]) else: return(n_2[0]+' '+n_1[ge[1]]) else: if ge[1]==0: return(n_1[ge[0]]+' '+n_2[0]) else: return(n_1[ge[0]]+' '+n_2[0]+' '+n_1[ge[1]]) else: return(n_1[ge[0]]) def reader_wan(wan,ge): wan=list(map(int,wan)) ge=list(map(int,ge)) if len(wan) == 4: if wan[0]==0: if wan[1]==0 and wan[2]==0 and wan[3]==0: if ge[0]!=0: return('ling') else: return('') elif wan[1]==0 and wan[2]==0 and wan[3]!=0: return(n_1[wan[0]]+' '+n_1[wan[3]]+' '+n_2[3]) elif wan[1]==0 and wan[2]!=0 and wan[3]==0: return(n_1[wan[0]]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_2[3]) elif wan[1]==0 and wan[2]!=0 and wan[3]!=0: return(n_1[wan[0]]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_1[wan[3]]+' '+n_2[3]) elif wan[1]!=0 and wan[2]==0 and wan[3]==0: return(n_1[wan[0]]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_2[3]) elif wan[1]!=0 and wan[2]==0 and wan[3]!=0: return(n_1[wan[0]]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_1[wan[2]]+' '+n_1[wan[3]]+' '+n_2[3]) elif wan[1]!=0 and wan[2]!=0 and wan[3]==0: return(n_1[wan[0]]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_2[3]) else: return(n_1[wan[0]]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_1[wan[3]]+' '+n_2[3]) else: if wan[1]==0 and wan[2]==0 and wan[3]==0: return(n_1[wan[0]]+' '+n_2[2]+' '+n_2[3]) elif wan[1]==0 and wan[2]==0 and wan[3]!=0: return(n_1[wan[0]]+' '+n_2[2]+' '+n_1[wan[1]]+' '+n_1[wan[3]]+' '+n_2[3]) elif wan[1]==0 and wan[2]!=0 and wan[3]==0: return(n_1[wan[0]]+' '+n_2[2]+' '+n_1[wan[1]]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_2[3]) elif wan[1]==0 and wan[2]!=0 and wan[3]!=0: return(n_1[wan[0]]+' '+n_2[2]+' '+n_1[wan[1]]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_1[wan[3]]+' '+n_2[3]) elif wan[1]!=0 and wan[2]==0 and wan[3]==0: return(n_1[wan[0]]+' '+n_2[2]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_2[3]) elif wan[1]!=0 and wan[2]==0 and wan[3]!=0: return(n_1[wan[0]]+' '+n_2[2]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_1[wan[2]]+' '+n_1[wan[3]]+' '+n_2[3]) elif wan[1]!=0 and wan[2]!=0 and wan[3]==0: return(n_1[wan[0]]+' '+n_2[2]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_2[3]) else: return(n_1[wan[0]]+' '+n_2[2]+' '+n_1[wan[1]]+' '+n_2[1]+' '+n_1[wan[2]]+' '+n_2[0]+' '+n_1[wan[3]]+' '+n_2[3]) elif len(wan) == 3: if wan[1] == 0 and wan[2] == 0: return(n_1[wan[0]]+' '+n_2[1]+' '+n_2[3]) elif wan[1] == 0 and wan[2] !=0: return(n_1[wan[0]]+' '+n_2[1]+' '+n_1[wan[1]]+' '+n_1[wan[2]]+' '+n_2[3]) elif ge[1]!=0 and ge[2]==0: return(n_1[ge[0]]+' '+n_2[1]+' '+n_1[ge[1]]+' '+n_2[0]+' '+n_2[3]) else: return(n_1[wan[0]]+' '+n_2[1]+' '+n_1[wan[1]]+' '+n_2[0]+' '+n_1[wan[2]]+' '+n_2[3]) elif len(wan) == 2: if wan[0]==1: if wan[1]==0: return(n_2[0]+' '+n_2[3]) else: return(n_2[0]+' '+n_1[wan[1]]+' '+n_2[3]) else: if wan[1]==0: return(n_1[wan[0]]+' '+n_2[0]+' '+n_2[3]) else: return(n_1[wan[0]]+' '+n_2[0]+' '+n_1[wan[1]]+' '+n_2[3]) else: return(n_1[wan[0]]+' '+n_2[3]) def reader_yi(yi): yi=list(map(int,yi)) if len(yi) == 4: if yi[1]==0 and yi[2]==0 and yi[3]==0: return(n_1[yi[0]]+' '+n_2[2]+' '+n_2[4]) elif yi[1]==0 and yi[2]==0 and yi[3]!=0: return(n_1[yi[0]]+' '+n_2[2]+' '+n_1[yi[1]]+' '+n_1[yi[3]]+' '+n_2[4]) elif yi[1]==0 and yi[2]!=0 and yi[3]==0: return(n_1[yi[0]]+' '+n_2[2]+' '+n_1[yi[1]]+' '+n_1[yi[2]]+' '+n_2[0]+' '+n_2[4]) elif yi[1]==0 and yi[2]!=0 and yi[3]!=0: return(n_1[yi[0]]+' '+n_2[2]+' '+n_1[yi[1]]+' '+n_1[yi[2]]+' '+n_2[0]+' '+n_1[yi[3]]+' '+n_2[4]) elif yi[1]!=0 and yi[2]==0 and yi[3]==0: return(n_1[yi[0]]+' '+n_2[2]+' '+n_1[yi[1]]+' '+n_2[1]+' '+n_2[4]) elif yi[1]!=0 and yi[2]==0 and yi[3]!=0: return(n_1[yi[0]]+' '+n_2[2]+' '+n_1[yi[1]]+' '+n_2[1]+' '+n_1[yi[2]]+' '+n_1[yi[3]]+' '+n_2[4]) elif yi[1]!=0 and yi[2]!=0 and yi[3]==0: return(n_1[yi[0]]+' '+n_2[2]+' '+n_1[yi[1]]+' '+n_2[1]+' '+n_1[yi[2]]+' '+n_2[0]+' '+n_2[4]) else: return(n_1[yi[0]]+' '+n_2[2]+' '+n_1[yi[1]]+' '+n_2[1]+' '+n_1[yi[2]]+' '+n_2[0]+' '+n_1[yi[3]]+' '+n_2[4]) elif len(yi) == 3: if yi[1] == 0 and yi[2] == 0: return(n_1[yi[0]]+' '+n_2[1]+' '+n_2[4]) elif yi[1] == 0 and yi[2] !=0: return(n_1[yi[0]]+' '+n_2[1]+' '+n_1[yi[1]]+' '+n_1[yi[2]]+' '+n_2[4]) elif yi[1]!=0 and yi[2]==0: return(n_1[yi[0]]+' '+n_2[1]+' '+n_1[yi[1]]+' '+n_2[0]+' '+n_2[4]) else: return(n_1[yi[0]]+' '+n_2[1]+' '+n_1[yi[1]]+' '+n_2[0]+' '+n_1[yi[2]]+' '+n_2[4]) elif len(yi) == 2: if yi[0]==1: if yi[1]==0: return(n_2[0]+' '+n_2[4]) else: return(n_2[0]+' '+n_1[yi[1]]+' '+n_2[4]) else: if yi[1]==0: return(n_1[yi[0]]+' '+n_2[0]+' '+n_2[4]) else: return(n_1[yi[0]]+' '+n_2[0]+' '+n_1[yi[1]]+' '+n_2[4]) else: return(n_1[yi[0]]+' '+n_2[4]) a=list(input().strip()) if len(a) > 8: ge=a[-4::] wan=a[-8:-4:1] yi=a[0:-8:1] print(reader_yi(yi)+' '+reader_wan(wan,ge)+' '+reader_ge(ge)) elif len(a) >4 and len(a)<9: ge=a[-4::] wan=a[0:-4:1] print(reader_wan(wan,ge)+' '+reader_ge(ge)) else: ge=a print(reader_ge(ge))
0.0分
4 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复