mso-hansi-font-family:"Times New Roman"'>今天在網上同學求教1 5 6
7 mso-hansi-font-family:"Times New Roman"'>用+-*/ "Times New Roman"'>算出21.yes"> "Times New Roman";mso-hansi-font-family:"Times New Roman"'>自己曾經寫過一個,mso-hansi-font-family:"Times New Roman"'>但代碼找不到了,"Times New Roman"'>偶知道24宋體;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>點的程式很多的說, mso-hansi-font-family:"Times New Roman"'>於是到網上搜了一下.
mso-hansi-font-family:"Times New Roman"'>是有不少, "Times New Roman"'>但是一個用c++"Times New Roman"'>的(宋體;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>其實根本就不能叫用c++,mso-hansi-font-family:"Times New Roman"'>全是c"Times New Roman"'>的文法), 宋體;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>試了nmso-hansi-font-family:"Times New Roman"'>多次,borlandc3.1"Times New Roman"'>和gcc宋體;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>都不能編譯. mso-hansi-font-family:"Times New Roman"'>還找到了vb,"Times New Roman"'>和web宋體;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>版的,mso-hansi-font-family:"Times New Roman"'>看來都沒用. mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>在偶找東西搞得焦頭爛額的時候,mso-hansi-font-family:"Times New Roman"'>偶同學自己算出來了.("Times New Roman"'>到底怎麼算用這個程式試試吧)
mso-hansi-font-family:"Times New Roman"'>為了以後不被這種問題困擾,"Times New Roman"'>花一個小時用python"Times New Roman"'>自己寫了一個,"Times New Roman"'>還是python"Times New Roman"'>好~~~~~~~
funs = [ lambda x, item: (x+item[0],
yes">
str(x)+'+('+item[1]+')'
yes">
),
yes"> lambda x, item: (x-item[0],
yes">
str(x)+'-('+item[1]+')'
yes">
),
yes"> lambda x, item: (item[0]-x,
yes"> yes"> '('+item[1]+')-'+str(x)
yes">
),
yes"> lambda x, item: (x*item[0],
yes">
str(x)+'*('+item[1]+')'
yes">
),
yes"> lambda x, item: (item[0]==0 and (0,'ZZZ')) or /
yes"> (x/item[0],
yes">
str(x)+'/('+item[1]+')'
yes">
),
yes"> lambda x, item: (x==0 and (0,'ZZZ')) or /
yes">
(item[0]/x,
yes">
'('+item[1]+')/'+str(x)
yes">
)
]
def con(num):
yes"> l = len(num)
yes"> p = list()
yes"> if l==1: return {num[0]:str(num[0])}
yes"> for i in range(l):
yes"> for f in funs:
yes">
p += map(lambda item: f(num[i],item),
yes">
con(num[:i]+num[i+1:]).items()
yes">
)
yes"> return dict(p)
print con(map(float,[1,5,6,7])).get(21.0,0)
mso-hansi-font-family:"Times New Roman"'>代碼我就不解釋了,"Times New Roman"'>有問題就問吧.
mso-hansi-font-family:"Times New Roman"'>另外由於浮點計算的誤差問題,".get(21.0,0""Times New Roman"'>這句還不太完善,"Times New Roman"'>不過解決這個問題足夠了,"Times New Roman"'>具體怎麼完善大家都知道拉.