一瓶汽水2元,2空瓶可換1汽水,4瓶蓋可換1汽水,問10元能喝多少瓶汽水
1 def duihuan(pingzi,gaizi,qian,jiu): 2 pingzi = pingzi+(qian/2) 3 gaizi = gaizi+(qian/2) 4 jiu = jiu+qian/2 5 qian = qian%2 6 7 gaizi = gaizi+pingzi/2 8 jiu = jiu+pingzi/2 9 pingzi = pingzi%2+pingzi/2 10 11 pingzi = pingzi+gaizi/4 12 jiu = jiu+gaizi/4 13 gaizi = gaizi%4+gaizi/4 14 print pingzi,"--",gaizi,"--",jiu,"--",qian 15 result = [pingzi,gaizi,qian,jiu] 16 return result 17 18 aaa = duihuan(0,0,10,0) 19 while(aaa[0]>=2 or aaa[1]>=4 or aaa[2]>=2): 20 aaa = duihuan(aaa[0],aaa[1],aaa[2],aaa[3])
結果:
4 -- 4 -- 8 -- 0
3 -- 3 -- 11 -- 0
3 -- 1 -- 13 -- 0
2 -- 2 -- 14 -- 0
1 -- 3 -- 15 -- 0
[Finished in 0.4s]