使用 “穷举法” 求解鸡兔同笼,Python 代码实现如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| def chicken_and_rabbit(head: int, foot: int) -> list:
if foot % 2 != 0:
raise ValueError("参数 foot 必须为偶数")
result = []
rabbit, chicken = 0, 0
while rabbit < head:
rabbit += 1
chicken = head - rabbit
# print(f"chicken: {chicken}, rabbit: {rabbit}", rabbit * 4 + chicken * 2)
if rabbit * 4 + chicken * 2 == foot:
result.append({"chicken": chicken, "rabbit": rabbit})
return result
def main(head: int, foot: int):
results = chicken_and_rabbit(head, foot)
if not results:
print("无解")
else:
for res in results:
print(res)
if __name__ == "__main__":
main(30, 88)
|