Crossin的编程教室
标题:
【每周一坑】鸡兔同笼
[打印本页]
作者:
cczztt
时间:
2018-7-27 15:36
标题:
【每周一坑】鸡兔同笼
from sympy import symbols, solve
from math import modf
def isInt(inp):
while True:
try:
inp = float(inp)
dec = modf(inp)
if dec[0] == 0:
return inp
break
else:
inp = input("Input Error! Try Again: ")
except:
inp = input("Input Error! Try again: ")
head = input("Input heads: ")
head = isInt(head)
foot = input("Input foot: ")
foot = isInt(foot)
result = {}
x = symbols('x')
y = symbols('y')
result = solve([x + y - head, 2 * x + 4 * y - foot], [x, y])
print(result)
if result[x] >= 0 and result[y] >= 0 and modf(result[x])[0] + modf(result[y])[0] == 0:
print("Chicken: %d, rabbits: %d."%(result[x], result[y]))
else:
print("Unsolvable!")
复制代码
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/)
Powered by Discuz! X2.5