[蓝桥杯] 寒假作业 (Python 实现)

x33g5p2x  于2022-02-22 转载在 Python  
字(0.5k)|赞(0)|评价(0)|浏览(359)

题目:

代码:

  1. nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  2. res = 0
  3. def dfs(nums, temp):
  4. global res
  5. if len(temp) == 3:
  6. if not check(temp, 3):
  7. return
  8. elif len(temp) == 6:
  9. if not check(temp, 6):
  10. return
  11. elif len(temp) == 9:
  12. if not check(temp,9):
  13. return
  14. elif len(temp) == 12:
  15. if not check(temp,12):
  16. return
  17. else:
  18. res += 1
  19. return
  20. for i in nums:
  21. new_nums = nums.copy()
  22. new_nums.remove(i)
  23. new_temp = temp + [i]
  24. dfs(new_nums,new_temp)
  25. def check(temp, n):
  26. if n == 3:
  27. return temp[0] + temp[1] == temp[2]
  28. elif n == 6:
  29. return temp[3] - temp[4] == temp[5]
  30. elif n == 9:
  31. return temp[6] * temp[7] == temp[8]
  32. elif n == 12:
  33. return temp[11] * temp[10] == temp[9]
  34. dfs(nums, [])
  35. print(res)

结果:64

相关文章