以下是一个简单的用Python编写的鸡兔同笼程序示例:
def calculate_animals(heads, legs): for num_chickens in range(heads+1): num_rabbits = heads - num_chickens if 2*num_chickens + 4*num_rabbits == legs: return num_chickens, num_rabbits return None, None heads = int(input("请输入头的数量:")) legs = int(input("请输入腿的数量:")) num_chickens, num_rabbits = calculate_animals(heads, legs) if num_chickens is not None: print("鸡的数量为:", num_chickens) print("兔的数量为:", num_rabbits) else: print("无法确定鸡兔的数量")
你可以在命令行中运行这个程序,输入头的数量和腿的数量,程序会输出计算出的鸡和兔的数量。