正剧开始:
星历2016年01月22日 11:12:43, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起继续修炼。
小伟刚拿到了[人叫板老师]设计的练气期第七层功法:
那就开始修炼吧。
小伟看到了一张写着很多很大的数字的图:
这些数字到底怎么读呢? 小伟拿出了读数字神器:
<span style="font-size:18px;">>>> 一千九百六十一万二千三百六十八 三百万二千一百六十六 八千零四十一万八千二百 九千四百零二万三千五百六十七 二千一百八十一万三千三百三十四 三千八百三十一万二千二百二十四 if __name__ == '__main__': a = [19612368, 3002166, 80418200, 94023567, 21813334, 38312224]; for i in range(len(a)): print(numberToChinese(a[i], ''));</span>
<span style="font-size:18px;">>>> 二千四百九十六 二千四百九十六万 三百零八万 四千零五十万 if __name__ == '__main__': a = [2496, 24960000, 3080000, 40500000]; for i in range(len(a)): print(numberToChinese(a[i], ''));</span>
<span style="font-size:18px;">>>> 五万四千六百二十一 六百四十万七千 一千零三万零四十 if __name__ == '__main__': a = [54621, 6407000, 10030040]; for i in range(len(a)): print(numberToChinese(a[i], ''));</span>
<span style="font-size:18px;">>>> 十万二千三百四十五 102345 二十三万零一百八十四 230184 三百零二万六千 3026000 二千零四十万零七百 20400700 ### # @usage 数字的中文写法转化为数字 # @author mw # @date 2016年01月08日 星期五 09:19:55 # @param # @return # ### def chineseToNumber(s): chineseOfNumber=['零','一', '二', '三', '四', '五', '六',\ '七', '八', '九', '十','百','千','万','亿']; result = 0; #每一组两个数,比如九百,一万,都是由一个值数和一个倍数组成。 #不可能两个以上的值在一块,但可能两个以上的倍数在一块,比如九九不合法,但四百万合法。 #合法表达为0,不合法为其它值 illegal = 0; #两个长度 lengthOfStr = len(s); lengthOfChs = len(chineseOfNumber); #合法性判断 for i in range(lengthOfStr): if illegal == 1: break; for j in range(lengthOfChs): if s[i] == chineseOfNumber[j]: break; else: if j >= lengthOfChs-1: print('含有非中文数字的字符,表达式不合法'); illegal = 1; for i in range(lengthOfStr-1): if illegal == 1: break; for j in range(10): if s[i] == chineseOfNumber[j]: if j>0: for k in range(10): if s[i+1] == chineseOfNumber[k]: print('连续两个本数相连而没有倍数,表达式不合法。'); illegal = 1; break; #当这个数是零时,它后面跟零或倍数都不合法 else: if s[i+1] == chineseOfNumber[0]: print('连续两个零相连,表达式不合法。'); illegal = 1; break; for k in range(10, lengthOfChs): if s[i+1] == chineseOfNumber[k]: print('零后面跟上倍数,表达式不合法。'); illegal = 1; break; for i in range(lengthOfStr-1): if illegal == 1: if (i > 0): print('表达式的倍数排序不符合规范,不合法。'); break; if s[i] == '十': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '百': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '千': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '万': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '亿': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千' or s[i+1] == '万': illegal = 1; else: pass; #合法则计算 if illegal!=0: print('输入不合法。'); else: value = 0; multiple = 1; result = 0; #超过亿的部分,单独分出来的原因是避免再和万的倍数相乘 yiPart = 0; #超过万的部分 wanPart = 0; for i in range(lengthOfStr): if s[i] == '亿': result += value+wanPart+yiPart; multiple = 100000000; value = result; result = value*multiple; if (i < lengthOfStr-1 and s[i+1] == '亿'): value = 0; else: yiPart = result; result = 0; multiple = 1; elif s[i] == '万': result += value+wanPart; multiple = 10000; value = result; result = value*multiple; if (i < lengthOfStr-1 and (s[i+1] == '亿' or s[i+1] == '万')): value = 0; else: if (result > 100000000): yiPart = result; else: wanPart = result; result = 0; multiple = 1; elif s[i] == '千': multiple = 1000; result += value*multiple; value = 0; multiple = 1; elif s[i] == '百': multiple = 100; result += value*multiple; value = 0; multiple = 1; #十这个数字,即可以作为本数,也可以作为倍数 elif s[i] == '十': if value == 0: value = 10; multiple = 1; result += value*multiple; value = 0; else: multiple = 10; result += value*multiple; value = 0; multiple = 1; else: for j in range(10): if s[i] == chineseOfNumber[j]: value = j; multiple = 1; if i >= lengthOfStr-1: result += value * multiple; result += wanPart + yiPart; print('{0} {1}'.format(s, result)); #return result; if __name__ == '__main__': a = ['十万二千三百四十五','二十三万零一百八十四', '三百零二万六千', '二千零四十万零七百']; for i in range(len(a)): chineseToNumber(a[i]);</span>
<span style="font-size:18px;">>>> 三百二十六万七千五百 3267500 四万零九十 40090 九千零二十万零三百 90200300 一百万 1000000 if __name__ == '__main__': a = ['三百二十六万七千五百', '四万零九十', '九千零二十万零三百', '一百万']; for i in range(len(a)): chineseToNumber(a[i]);</span>
<span style="font-size:18px;">>>> 四千二百万 42000000 四千零七万五千七百 40075700 十二万五千 125000 if __name__ == '__main__': a = ['四千二百万', '四千零七万五千七百', '十二万五千']; for i in range(len(a)): chineseToNumber(a[i]);</span>
<span style="font-size:18px;">>>> 三百六十万二千 3602000 五十四万零三百七十 540370 六万八千九百二十 68920 四千六百四十一万 46410000 十万零五 100005 一千零五十万零三十 10500030 if __name__ == '__main__': a = ['三百六十万二千','五十四万零三百七十',\ '六万八千九百二十', '四千六百四十一万', '十万零五', '一千零五十万零三十']; for i in range(len(a)): chineseToNumber(a[i]); >>> 三百六十万二千 五十四万零三百七十 六万八千九百二十 四千六百四十一万 一十万零五 一千零五十万零三十 if __name__ == '__main__': a = [3602000, 540370, 68920,46410000,100005,10500030]; for i in range(len(a)): print(numberToChinese(a[i], '')); ### # @usage 数字的中文写法 # @author mw # @date 2016年01月08日 星期五 12:37:26 # @param # @return # ### def numberToChinese(num, s): if (num < 0): num = abs(num); chineseOfNumber=['零','一', '二', '三', '四', '五', '六',\ '七', '八', '九', '十','百','千','万','亿']; bit = 0; tmp = num; if (tmp == 0): s = chineseOfNumber[0]; while (tmp > 0): tmp = tmp//10; bit+=1; tmp = num; while (tmp > 0): if (tmp < 10): s += chineseOfNumber[tmp]; tmp -= 10; elif (tmp < 100): s += chineseOfNumber[tmp//10]; s += '十'; tmp = tmp%10; elif (tmp < 1000): s += chineseOfNumber[tmp//100]; s += '百'; tmp = tmp%100; if tmp < 10 and tmp > 0: s += '零'; elif (tmp < 10000): s += chineseOfNumber[tmp//1000]; s += '千'; tmp = tmp%1000; if tmp < 100 and tmp > 0: s += '零'; elif (tmp < 100000000): s1 = ''; s += numberToChinese(tmp//10000, s1); s += '万'; tmp =tmp%10000; if tmp < 1000 and tmp > 0: s += '零'; elif (tmp >= 100000000): s1 = ''; s += numberToChinese(tmp//100000000, s1); s += '亿'; tmp = tmp%100000000; if tmp < 10000000 and tmp > 0: s += '零'; else: pass; return s;</span>
<span style="font-size:18px;">>>> 二十九万九千八百 299800 二万 20000</span>
<span style="font-size:18px;">>>> 读两个零的数字共有120个,分别是:[1020034, 1020043, 1020340, 1020430, 1023004, 1023040, 1024003, 1024030, 1030024, 1030042, 1030240, 1030420, 1032004, 1032040, 1034002, 1034020, 1040023, 1040032, 1040230, 1040320, 1042003, 1042030, 1043002, 1043020, 1200304, 1200403, 1300204, 1300402, 1400203, 1400302, 2010034, 2010043, 2010340, 2010430, 2013004, 2013040, 2014003, 2014030, 2030014, 2030041, 2030140, 2030410, 2031004, 2031040, 2034001, 2034010, 2040013, 2040031, 2040130, 2040310, 2041003, 2041030, 2043001, 2043010, 2100304, 2100403, 2300104, 2300401, 2400103, 2400301, 3010024, 3010042, 3010240, 3010420, 3012004, 3012040, 3014002, 3014020, 3020014, 3020041, 3020140, 3020410, 3021004, 3021040, 3024001, 3024010, 3040012, 3040021, 3040120, 3040210, 3041002, 3041020, 3042001, 3042010, 3100204, 3100402, 3200104, 3200401, 3400102, 3400201, 4010023, 4010032, 4010230, 4010320, 4012003, 4012030, 4013002, 4013020, 4020013, 4020031, 4020130, 4020310, 4021003, 4021030, 4023001, 4023010, 4030012, 4030021, 4030120, 4030210, 4031002, 4031020, 4032001, 4032010, 4100203, 4100302, 4200103, 4200301, 4300102, 4300201] def tmp(): a = [0,0,0,1,2,3,4]; array = perm(a); size = len(array); size1 = len(array[0]); num = 0; result = set([]); for i in range(size): num = 0; for j in range(size1): num *= 10; num += array[i][j]; if num >= pow(10, 6): s = numberToChinese(num, ''); if (s.count('零') == 2): result.add(num); listA = list(result); listA.sort(); print('读两个零的数字共有{0}个,分别是:{1}'.format(len(listA), listA)); >>> 读一个零的数字共有264个,分别是:[1000234, 1000243, 1000324, 1000342, 1000423, 1000432, 1002034, 1002043, 1002304, 1002403, 1003024, 1003042, 1003204, 1003402, 1004023, 1004032, 1004203, 1004302, 1023400, 1024300, 1032400, 1034200, 1042300, 1043200, 1200034, 1200043, 1200340, 1200430, 1203004, 1203040, 1204003, 1204030, 1230004, 1230040, 1230400, 1240003, 1240030, 1240300, 1300024, 1300042, 1300240, 1300420, 1302004, 1302040, 1304002, 1304020, 1320004, 1320040, 1320400, 1340002, 1340020, 1340200, 1400023, 1400032, 1400230, 1400320, 1402003, 1402030, 1403002, 1403020, 1420003, 1420030, 1420300, 1430002, 1430020, 1430200, 2000134, 2000143, 2000314, 2000341, 2000413, 2000431, 2001034, 2001043, 2001304, 2001403, 2003014, 2003041, 2003104, 2003401, 2004013, 2004031, 2004103, 2004301, 2013400, 2014300, 2031400, 2034100, 2041300, 2043100, 2100034, 2100043, 2100340, 2100430, 2103004, 2103040, 2104003, 2104030, 2130004, 2130040, 2130400, 2140003, 2140030, 2140300, 2300014, 2300041, 2300140, 2300410, 2301004, 2301040, 2304001, 2304010, 2310004, 2310040, 2310400, 2340001, 2340010, 2340100, 2400013, 2400031, 2400130, 2400310, 2401003, 2401030, 2403001, 2403010, 2410003, 2410030, 2410300, 2430001, 2430010, 2430100, 3000124, 3000142, 3000214, 3000241, 3000412, 3000421, 3001024, 3001042, 3001204, 3001402, 3002014, 3002041, 3002104, 3002401, 3004012, 3004021, 3004102, 3004201, 3012400, 3014200, 3021400, 3024100, 3041200, 3042100, 3100024, 3100042, 3100240, 3100420, 3102004, 3102040, 3104002, 3104020, 3120004, 3120040, 3120400, 3140002, 3140020, 3140200, 3200014, 3200041, 3200140, 3200410, 3201004, 3201040, 3204001, 3204010, 3210004, 3210040, 3210400, 3240001, 3240010, 3240100, 3400012, 3400021, 3400120, 3400210, 3401002, 3401020, 3402001, 3402010, 3410002, 3410020, 3410200, 3420001, 3420010, 3420100, 4000123, 4000132, 4000213, 4000231, 4000312, 4000321, 4001023, 4001032, 4001203, 4001302, 4002013, 4002031, 4002103, 4002301, 4003012, 4003021, 4003102, 4003201, 4012300, 4013200, 4021300, 4023100, 4031200, 4032100, 4100023, 4100032, 4100230, 4100320, 4102003, 4102030, 4103002, 4103020, 4120003, 4120030, 4120300, 4130002, 4130020, 4130200, 4200013, 4200031, 4200130, 4200310, 4201003, 4201030, 4203001, 4203010, 4210003, 4210030, 4210300, 4230001, 4230010, 4230100, 4300012, 4300021, 4300120, 4300210, 4301002, 4301020, 4302001, 4302010, 4310002, 4310020, 4310200, 4320001, 4320010, 4320100] >>> 所有零都不读的数字共有72个,分别是:[1002340, 1002430, 1003240, 1003420, 1004230, 1004320, 1203400, 1204300, 1234000, 1243000, 1302400, 1304200, 1324000, 1342000, 1402300, 1403200, 1423000, 1432000, 2001340, 2001430, 2003140, 2003410, 2004130, 2004310, 2103400, 2104300, 2134000, 2143000, 2301400, 2304100, 2314000, 2341000, 2401300, 2403100, 2413000, 2431000, 3001240, 3001420, 3002140, 3002410, 3004120, 3004210, 3102400, 3104200, 3124000, 3142000, 3201400, 3204100, 3214000, 3241000, 3401200, 3402100, 3412000, 3421000, 4001230, 4001320, 4002130, 4002310, 4003120, 4003210, 4102300, 4103200, 4123000, 4132000, 4201300, 4203100, 4213000, 4231000, 4301200, 4302100, 4312000, 4321000] def tmp(): a = [0,0,0,1,2,3,4]; array = perm(a); size = len(array); size1 = len(array[0]); num = 0; result = set([]); for i in range(size): num = 0; for j in range(size1): num *= 10; num += array[i][j]; if num >= pow(10, 6): s = numberToChinese(num, ''); if (s.count('零') == 0): result.add(num); listA = list(result); listA.sort(); print('所有零都不读的数字共有{0}个,分别是:{1}'.format(len(listA), listA)); >>> 读三个零的数字共有24个,分别是:[1020304, 1020403, 1030204, 1030402, 1040203, 1040302, 2010304, 2010403, 2030104, 2030401, 2040103, 2040301, 3010204, 3010402, 3020104, 3020401, 3040102, 3040201, 4010203, 4010302, 4020103, 4020301, 4030102, 4030201] def tmp(): a = [0,0,0,1,2,3,4]; array = perm(a); size = len(array); size1 = len(array[0]); num = 0; result = set([]); for i in range(size): num = 0; for j in range(size1): num *= 10; num += array[i][j]; if num >= pow(10, 6): s = numberToChinese(num, ''); if (s.count('零') == 3): result.add(num); listA = list(result); listA.sort(); print('读三个零的数字共有{0}个,分别是:{1}'.format(len(listA), listA));</span>
<span style="font-size:18px;">>>> 七十亿 一百亿四千万二千 四千零三亿零五百万 if __name__ == '__main__': a = [7000000000, 10040002000, 400305000000]; for i in range(len(a)): print(numberToChinese(a[i], ''));</span>
<span style="font-size:18px;">>>> 三亿 300000000 七千零三亿零二十万 700300200000 if __name__ == '__main__': a = ['三亿', '七千零三亿零二十万']; for i in range(len(a)): chineseToNumber(a[i]);</span>
本节到此结束,欲知后事如何,请看下回分解。