'''
问题:鸡兔同笼 用户输入 h f
已知:同一个笼子里 鸡兔共h只 总脚数f只
1求解
2循环枚举
'''
def fa1(h, f):
#设鸡c只 兔r只
if(h > 0 and f >0 and h % 2 == 0 and f % 2 == 0):
r = f / 2 - h
c = h - r
print("鸡{0}只 兔{1}只".format(c, r))
else:
print("无解请重新测试")
def fa2(h, f):
if(h > 0 and f >0 and h % 2 == 0 and f % 2 == 0):
for c in range(h):
r = h - c
if(c * 2 + r * 4 == f):
print("鸡{0}只 兔{1}只".format(c, r))
else:
print("无解,请重新测试")
def main():
h = int(input("请输入一只笼子中的动物总数:"))
f = int(input("请输入总脚数:"))
fa1(h, f)
fa2(h, f)
main()
Python_鸡兔同笼
最新推荐文章于 2024-03-02 13:40:23 发布