題目描述:
銀行在列印票據的時候,常常需要將阿拉伯數字表示的人民幣金額轉換為大寫表示,現在請你來完成這樣乙個程式。在中文大寫方式中,0到10以及100、1000、10000被依次表示為: 零 壹 貳 叄 肆 伍 陸 柒 捌 玖 拾 佰 仟 萬
以下的例子示範了阿拉伯數字到人民幣大寫的轉換規則:
1 壹圓
11 壹拾壹圓
111 壹佰壹拾壹圓
101 壹佰零壹圓
-1000 負壹仟圓
1234567 壹佰貳拾叄萬肆仟伍佰陸拾柒圓
現在給你乙個整數a(|a|<100000000), 請你列印出人民幣大寫表示.
例如:a=1
則輸出:壹圓
這題目最頭疼的地方是當一串數字中間和末尾都存在0的時候,要去掉重複出現的0和對應的單位。
例如: 6100 0310元
對應的大寫為: 陸仟壹佰萬零叄佰壹拾圓
這裡6100後面的零轉換成大寫之後都去掉了,0310個位數上的0也去掉了,但是千位上的數字0保留。
針對題目中的最高八位數的轉換,我的思路是先考慮簡單的,當輸入的數字是1,2,3,或者4位數時,就簡單羅列出情況即可,其中4位數時情況較為複雜。以4位數為例,主要考慮0的個數0的位置,一共有如下的16種情況:(以字母n表示該位置是除0以外的其他數字)
nnnn
nnn0
nn0n
nn00
n0nn
n0n0
n00n
n000
0nnn
0nn0
0n0n
0n00
00nn
00n0
000n
0000
這裡我把這16種情況分別寫進了程式裡,待會兒我去看看大神是怎麼寫的,完整程式如下:
對比一下大神們的操作,自己的**簡直弱爆了,請睜大雙眼# -*- coding: utf-8 -*-
import itertools
a=-4603100;
c=l=;
k=0;
def auto_4(l2,l3):
if l2[0]==0:
if l2[1]==0:
if l2[2]==0:
if l2[3]==0:
del l3[0:7];
else:
del l3[1:6];
else:
if l2[3]==0:
del (l3[1:4],l3[3]);
else:
del l3[1:4];
else: #l[1]!=0 討論四種情況
if l2[2]==0:
if l2[3]==0:
del (l3[0:2],l3[2:5]);
else:
del (l3[0:2],l3[2:4]);
else:
if l2[3]==0:
del (l3[0:2],l3[4]);
else:
del l3[0:2];
else:
if l2[1]==0:
if l2[2]==0:
if l2[3]==0:
del l3[2:7];
else:
del l3[3:6];
else:
if l2[3]==0:
del (l3[3],l3[5]);
else:
del l3[3];
else: #l[1]!=0 討論四種情況
if l2[2]==0:
if l2[3]==0:
del l3[4:7];
else:
del l3[4:6];
else:
if l2[3]==0:
del l3[6];
return l3
#判斷正負
if a<0:
a=-a;
k=1;#標誌位
#將乙個數轉成數字列表
while true:
(n,l)=divmod(a,10);
a=n;
if a==0:
break
l2=l[::-1]; #因為除法的原因,需要倒一下順序才是原來的是順序
nl=len(l2); #判斷數的長度
l3=;
for i in l2:
#在前四位數加上單位
def pp1(nl,l2,l3):
if nl==1:
l3.insert(1,'圓')
if nl==2:
l3.insert(1,'拾')
if l3[2]=='零':
del l3[2]
l3.insert(3,'圓')
if nl==3:
l3.insert(1,'佰')
if l3[2]!='零':
l3.insert(3,'拾')
if l3[4]=='零':
del l3[4]
else:
if l3[3]=='零':
del l3[2]
del l3[2]
l3.insert(5,'圓')
if nl==4:
l3.insert(1,'仟')
l3.insert(3,'佰')
l3.insert(5,'拾')
l3.insert(7,'圓')
auto_4(l2,l3);
return l3
#後四位數上加上單位
def pp2(nl,l2,l3):
if nl==1:
l3.insert(1,'萬')
if nl==2:
l3.insert(1,'拾')
if l3[2]=='零':
del l3[2]
l3.insert(3,'萬')
if nl==3:
l3.insert(1,'佰')
if l3[2]!='零':
l3.insert(3,'拾')
if l3[4]=='零':
del l3[4]
else:
if l3[3]=='零':
del l3[2]
del l3[2]
l3.insert(5,'萬')
if nl==4:
l3.insert(1,'仟')
l3.insert(3,'佰')
l3.insert(5,'拾')
l3.insert(7,'萬')
auto_4(l2,l3);
return l3
if nl<5:
q1=pp1(nl,l2,l3);
else:
m2=l2[-4::];
ll2=l3[-4::]; #取出後四位數
del(l3[-4::])
del(l2[-4::])
ll1=l3; #取出前面的幾位數
m1=l2;
nl1=len(ll1);
q2=pp1(4,m2,ll2);
q1=pp2(nl1,m1,ll1);
q1.extend(q2);
#根據正負加上符號
if k==1:
q1.insert(0,'負')
#把列表字元合併
s = "".join(itertools.chain(*q1))
print(s)
如上,我覺得自己的思路還是不夠巨集觀,不夠清楚,自己想到的是細節的東西,然後有用**去解決,上面大神的思路要更加大局一點,很清晰,我是通過分類討論的思路,而大神是通過檢測增添的思路。num_dic =
unit_dic =
temp =
t = str('%8d'%abs(a)) #重點:把數字換成固定長度8位的字串
for s in range(0,4):
if t[s] != ' ' and t[s] != '0':
if t[s] == '0' and s <3 and t[s+1] != '0':
if(t[2] != ' ' and t[3] == '0'):
print(t)
for s in range(4,8):
if t[s] != ' ' and t[s] != '0':
if t[s] == '0' and s <7 and t[s+1] != '0':
temp = ''.join(temp)
if a < 0:
temp = '負' + temp
if a == 0:
temp = '零' + temp
print(temp + u'圓')
下面這個10行**更是十分暴力,只能說其中enumerate()和zip()函式學到了,膜拜膜拜。
digit = [u'零',u'壹',u'貳',u'叄',u'肆',u'伍',u'陸',u'柒',u'捌',u'玖']
weight = [u'圓',u'拾',u'佰',u'仟',u'萬',u'拾',u'佰',u'仟']
z = [(u'零仟',u'零佰',u'零拾',u'零零零',u'零零',u'零萬',u'零圓'),(u'零',u'零',u'零',u'零',u'零',u'萬',u'圓')]
num = str(abs(a)) # 整數轉換成字串
s = u'負' if a<0 else ''
for i,x in enumerate(num):
s += digit[int(x)] + weight[len(num)-i-1]
for z,v in zip(z[0],z[1]):
s = s.replace(z,v)
print u'零圓' if a==0 else s
人民幣轉大寫
function changermb const strrmb string string vartxt,strhighlevel string i,n,m,ilen,ipos integer n記錄整數部分長度,m記錄分數部分長度 strarray,strlevel array of string...
人民幣小寫轉大寫
package day00 public class moneyelse result result.replaceall 零角 零 result result.replaceall 零分 零 result result.replaceall 角零 角 if result.equals 零零 ret...
人民幣小寫轉大寫
c code 例如 new money 200 tostring 貳佰元 namespace skyiv.util catch system.console.writeline 大寫 newmoney m 該類過載的 tostring 方法返回的是大寫金額字串 class money 建構函式 pu...