[关闭]
@LianSuo 2017-04-09T07:00:25.000000Z 字数 1680 阅读 658

Python第一次综合训练报告

Never Settle

小组信息

组长:徐鹏航

成员名单:徐鹏航,张伟琛,王名扬,郭彤苑

题目:计算24点

  1. # -*- coding:utf-8 -*-
  2. import itertools
  3. a1=[]
  4. for i in range(1,5):
  5. a1.append(str(raw_input('请输入第'+str(i)+'个数字')))
  6. for eachl in itertools.permutations(a1,4):
  7. strlist=[num for num in eachl]
  8. a=str(float(strlist[0]));b=str(float(strlist[1]));c=str(float(strlist[2]));d=str(float(strlist[3]))
  9. try:
  10. for fh1 in ['+','-','*','/']:
  11. for fh2 in ['+','-','*','/']:
  12. for fh3 in ['+','-','*','/']:
  13. if eval('('+a+fh1+b+')'+fh2+c+fh3+d)==24.0:
  14. print '('+a+fh1+b+')'+fh2+c+fh3+d,"=",eval('('+a+fh1+b+')'+fh2+c+fh3+d)
  15. if eval(a+fh1+'('+b+fh2+c+')'+fh3+d)==24.0:
  16. print a+fh1+'('+b+fh2+c+')'+fh3+d,"=",eval(a+fh1+'('+b+fh2+c+')'+fh3+d)
  17. if eval(a+fh1+b+fh2+'('+c+fh3+d+')')==24.0:
  18. print a+fh1+b+fh2+'('+c+fh3+d+')',"= ",eval(a+fh1+b+fh2+'('+c+fh3+d+')')
  19. if eval('('+a+fh1+b+fh2+c+')'+fh3+d)==24.0:
  20. print '('+a+fh1+b+fh2+c+')'+fh3+d,"=",eval('('+a+fh1+b+fh2+c+')'+fh3+d)
  21. if eval(a+fh1+'('+b+fh2+c+fh3+d+')')==24.0:
  22. print a+fh1+'('+b+fh2+c+fh3+d+')',"=",eval(a+fh1+'('+b+fh2+c+fh3+d+')')
  23. if eval('('+a+fh1+b+')'+fh2+'('+c+fh3+d+')')==24.0:
  24. print '('+a+fh1+b+')'+fh2+'('+c+fh3+d+')',"=",eval('('+a+fh1+b+')'+fh2+'('+c+fh3+d+')')
  25. if eval('('+'('+a+fh1+b+')'+fh2+c+')'+fh3+d)==24.0:
  26. print '('+'('+a+fh1+b+')'+fh2+c+')'+fh3+d,"=",eval('('+'('+a+fh1+b+')'+fh2+c+')'+fh3+d)
  27. if eval('('+a+fh1+'('+b+fh2+c+')'+')'+fh3+d)==24.0:
  28. print '('+a+fh1+'('+b+fh2+c+')'+')'+fh3+d,"=",eval('('+a+fh1+'('+b+fh2+c+')'+')'+fh3+d)
  29. if eval(a+fh1+'('+'('+b+fh2+c+')'+fh3+d+')')==24.0:
  30. print a+fh1+'('+'('+b+fh2+c+')'+fh3+d+')',"=",eval(a+fh1+'('+'('+b+fh2+c+')'+fh3+d+')')
  31. if eval(a+fh1+'('+b+fh2+'('+c+fh3+d+')'+')')==24.0:
  32. print a+fh1+'('+b+fh2+'('+c+fh3+d+')'+')',"=",eval(a+fh1+'('+b+fh2+'('+c+fh3+d+')'+')')
  33. except:
  34. ZeroDivisionError

感谢阅读

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注