python 的算24點程式

來源:互聯網
上載者:User


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"'>具體怎麼完善大家都知道拉.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.