以下是一個簡單的用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("無法確定雞兔的數量")
你可以在命令行中運行這個程序,輸入頭的數量和腿的數量,程序會輸出計算出的雞和兔的數量。