正剧开始:
星历2016年01月08日 14:20:17, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究万以内数的认识这节功法。
14:23:10, 小伟看到了一千以内的数的认识。
14:24:36, 小伟看到了[人叫板老师]教大家读数字。
可是小伟不想每次都自己去想该怎么读,怎么办呢,阿伟给小伟制作了一件神器,专门用来读数字:
<span style="font-size:18px;">### # @usage 数字的中文写法 # @author mw # @date 2016年01月08日 星期五 12:37:26 # @param # @return # ### def numberToChinese(num, s): chineseOfNumber=['零','一', '二', '三', '四', '五', '六',\ '七', '八', '九', '十','百','千','万','亿']; bit = 0; tmp = num; 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; if __name__ == '__main__': a = 235; print('{0} 读作 {1}'.format(a, numberToChinese(a, ''))); >>> 235 读作 二百三十五</span>
怎么着,这一堆堆的中文字,小伟都认识,但这是些什么数字呢?
没办法,阿伟只好又制作了一件神器,这次专门把中文数字写出来。
<span style="font-size:18px;">### # @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; littlePart = 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; if (i < lengthOfStr-1 and (s[i+1] == '亿' or s[i+1] == '万')): value = 0; multiple = 1; elif s[i] == '百': multiple = 100; result += value*multiple; if (i < lengthOfStr-1 and (s[i+1] == '亿' or s[i+1] == '万')): value = 0; multiple = 1; #十这个数字,即可以作为本数,也可以作为倍数 elif s[i] == '十': if value == 0: value = 10; multiple = 1; result += value*multiple; else: multiple = 10; result += value*multiple; if (i < lengthOfStr-1 and (s[i+1] == '亿' or s[i+1] == '万')): value = result; else: 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)); if __name__ == '__main__': chineseToNumber('八'); chineseToNumber('十'); chineseToNumber('十五'); chineseToNumber('一十'); chineseToNumber('二十二'); chineseToNumber('二百三十五'); chineseToNumber('一百八十七'); chineseToNumber('四百'); chineseToNumber('六百零五'); chineseToNumber('三百二十'); chineseToNumber('二百九十'); chineseToNumber('八百四十六'); chineseToNumber('八千七百零五'); chineseToNumber('八万零八百四十六'); chineseToNumber('八万一千八百四十六'); chineseToNumber('八十八万一千八百四十六'); chineseToNumber('八千万零八百四十六'); chineseToNumber('四万万五千万零八百四十六'); chineseToNumber('四亿零八万零八百四十六'); chineseToNumber('四亿亿五千万');</span>
<span style="font-size:18px;">一百八十七 187 四百 400 六百零五 605 三百二十 320 二百九十 290 八百四十六 846</span>
没办法,阿伟只好给小伟做算盘:
<span style="font-size:18px;">/** * @usage 算盘 Abacus * @author mw * @date 2016年01月08日 星期五 12:37:26 * @param * @return * */ function Abacus() { this.abacus = function(num, range) { plot.save(); if (range) { plot.scale(range/460, range/460); } var number = new Array([0, 0, 0, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1]); if (num) { for (var i = 12; i>=0; i--) { number[i] = num % 10; num = Math.floor(num / 10); if (num <= 0) { break; } } } var w = 420, h = 200, xcenter = 0, ycenter = 0, yBeam = ycenter - 39; var r = (h-20)/12; var beamH = 16; plot.setFillStyle('#CC0000'); shape.fillRect(0, 0, w+40, h+40); plot.setFillStyle('white'); shape.fillRect(0, 0, w, h); plot.setFillStyle('#FF6622'); shape.fillRect(0, -39, w, beamH); var gap = 32; for (var i = 0; i < 13; i++) { shape.fillRect(-193+gap*i, 0, 8, 200); } plot.setFillStyle('black'); var x, yUp, yUpBeam, yDown, yDownBeam; for (var i = 0; i < 13; i++) { x = -193+gap*i; yDown = h/2-r/2; yDownBeam = yBeam + beamH/2 + r/2; yUp = -h/2 + r/2; yUpBeam = yBeam - beamH/2 - r/2; switch (number[i]) { default: case 0: for (var j = 0; j<5; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<2; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } break; case 1: for (var j = 0; j<1; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<4; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<2; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } break; case 2: for (var j = 0; j<2; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<3; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<2; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } break; case 3: for (var j = 0; j<3; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<2; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<2; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } break; case 4: for (var j = 0; j<4; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<2; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } break; case 5: for (var j = 0; j<5; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUpBeam-j * r*1.1, r, r/2); } break; case 6: for (var j = 0; j<4; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUpBeam-j * r*1.1, r, r/2); } break; case 7: for (var j = 0; j<3; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<2; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUpBeam-j * r*1.1, r, r/2); } break; case 8: for (var j = 0; j<2; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<3; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUpBeam-j * r*1.1, r, r/2); } break; case 9: for (var j = 0; j<1; j++) { shape.fillEllipse(x, yDown-j * r*1.1, r, r/2); } for (var j = 0; j<4; j++) { shape.fillEllipse(x, yDownBeam+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUp+j * r*1.1, r, r/2); } for (var j = 0; j<1; j++) { shape.fillEllipse(x, yUpBeam-j * r*1.1, r, r/2); } break; } } plot.restore(); } this.demo = function() { return this.abacus(1234567890); } }</span>
来拨个563试试。
好在[人叫板老师]没有再在本节增加更多的新功法,这样小伟就可以开心地试验刚到手的几件神器了。
有了神器,不费吹灰之力:
<span style="font-size:18px;">function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,2,1,2); //config.axis2D(0, 0, 180); var abacus = new Abacus(); var a = [17, 254, 180, 309, 600]; var r = 200; var row = 3, col = 2; var count = 0; for (var i = 0; i < row; i++) { for (var j = 0; j < col; j++) { if (count < a.length) { config.setSector(row, col, i+1, j+1); abacus.abacus(a[count], r); count++; } } } }</span>
<span style="font-size:18px;">function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,2,1,2); //config.axis2D(0, 0, 180); var abacus = new Abacus(); var a = [270,538,406]; var r = 250; var row = 2, col = 2; var count = 0; for (var i = 0; i < row; i++) { for (var j = 0; j < col; j++) { if (count < a.length) { config.setSector(row, col, i+1, j+1); abacus.abacus(a[count], r); plot.fillText(a[count].toFixed(0), -30, 90, 100); count++; } } } }</span>
>>> a=4589;
>>> print('{0} 读作 {1}'.format(a, numberToChinese(a, '')));
4589 读作 四千五百八十九
>>> a=6772;
>>> print('{0} 读作 {1}'.format(a, numberToChinese(a, '')));
6772 读作 六千七百七十二
<span style="font-size:18px;">if __name__ == '__main__': a = [3745, 2080, 6009]; for i in range(len(a)): print('{0} 读作 {1}'.format(a[i], numberToChinese(a[i], ''))); >>> 3745 读作 三千七百四十五 2080 读作 二千零八十 6009 读作 六千零九</span>
>>>
7438 读作 七千四百三十八
3604 读作 三千六百零四
4900 读作 四千九百
5002 读作 五千零二
1050 读作 一千零五十
560 读作 五百六十
366 读作 三百六十六
2540 读作 二千五百四十
<span style="font-size:18px;">if __name__ == '__main__': a = [7438,3604,4900,5002,1050,560,366,2540]; for i in range(len(a)): print('{0} 读作 {1}'.format(a[i], numberToChinese(a[i], '')));</span>
<span style="font-size:18px;">if __name__ == '__main__': chineseToNumber('一千三百四十二'); chineseToNumber('三千零六十九'); chineseToNumber('七千零一'); chineseToNumber('二千七百'); chineseToNumber('一万');</span>
<span style="font-size:18px;">function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,2,1,2); //config.axis2D(0, 0, 180); var abacus = new Abacus(); var a = [4632,2508,9999]; var r = 250; var row = 2, col = 2; var count = 0; for (var i = 0; i < row; i++) { for (var j = 0; j < col; j++) { if (count < a.length) { config.setSector(row, col, i+1, j+1); abacus.abacus(a[count], r); plot.fillText(a[count].toFixed(0), -30, 90, 100); count++; } } } }</span>
>>>
1533 读作 一千五百三十三
1300 读作 一千三百
2155 读作 二千一百五十五
2016 读作 二千零一十六
1492 读作 一千四百九十二
阿伟才发现原来五岳并不是以前想象中的那样几千米的高山啊,最高才2000米多点。
<span style="font-size:18px;">if __name__ == '__main__': chineseToNumber('一百九十一'); chineseToNumber('六百八十六'); chineseToNumber('二千二百五十三'); chineseToNumber('五千九百一十九');</span>
突然,阿伟怔了一下,感觉到被[人叫板老师]玩了,这用4颗算珠能表示的四位数字,好像并不少啊。
阿伟吩咐小伟:给我算一下一共有多少个数字符合要求。
小伟答道:是,[阿伟大人]。
于是小伟进行了计算:
<span style="font-size:18px;">def tmp3(): need = [0, 1, 2, 3, 4, 1, 2, 3,4,5]; count = 0; total = 0; string = ''; for i in range(1000, 10000): count = 0; string = str(i); for j in range(4): count += need[int(string[j])]; if count == 4: print(i, end=' '); if total %10 == 9: print('\n'); total+=1; print('\n一共有{0:>3}个数字可以用4颗算珠表示出来。'.format(total));</span>
<span style="font-size:18px;">>>> 1003 1007 1012 1016 1021 1025 1030 1052 1056 1061 1065 1070 1102 1106 1111 1115 1120 1151 1155 1160 1201 1205 1210 1250 1300 1502 1506 1511 1515 1520 1551 1555 1560 1601 1605 1610 1650 1700 2002 2006 2011 2015 2020 2051 2055 2060 2101 2105 2110 2150 2200 2501 2505 2510 2550 2600 3001 3005 3010 3050 3100 3500 4000 5003 5007 5012 5016 5021 5025 5030 5052 5056 5061 5065 5070 5102 5106 5111 5115 5120 5151 5155 5160 5201 5205 5210 5250 5300 5502 5506 5511 5515 5520 5551 5555 5560 5601 5605 5610 5650 5700 6002 6006 6011 6015 6020 6051 6055 6060 6101 6105 6110 6150 6200 6501 6505 6510 6550 6600 7001 7005 7010 7050 7100 7500 8000 </span>
<span style="font-size:18px;"> 一共有126个数字可以用4颗算珠表示出来。</span>
到这里,几件神器的强大功能已经都得到验证,小伟和阿伟都很开心。
本节到此结束,欲知后事如何,请看下回分解。