Crossin的编程教室

标题: 【每周一坑】鸡兔同笼 [打印本页]

作者: cczztt    时间: 2018-7-27 15:36
标题: 【每周一坑】鸡兔同笼
  1. from sympy import symbols, solve
  2. from math import modf

  3. def isInt(inp):
  4.     while True:
  5.         try:
  6.             inp = float(inp)
  7.             dec = modf(inp)
  8.             if dec[0] == 0:
  9.                 return inp
  10.                 break
  11.             else:
  12.                 inp = input("Input Error! Try Again: ")
  13.         except:
  14.             inp = input("Input Error! Try again: ")

  15. head = input("Input heads: ")
  16. head = isInt(head)
  17. foot = input("Input foot: ")
  18. foot = isInt(foot)

  19. result = {}
  20. x = symbols('x')
  21. y = symbols('y')
  22. result = solve([x + y - head, 2 * x + 4 * y - foot], [x, y])
  23. print(result)
  24. if result[x] >= 0 and result[y] >= 0 and modf(result[x])[0] + modf(result[y])[0] == 0:
  25.     print("Chicken: %d, rabbits: %d."%(result[x], result[y]))
  26. else:
  27.     print("Unsolvable!")
复制代码





欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5