蛮力法如何求鸡和兔的个数
有一群鸡和一群兔,它们的只数相同,它们的脚数都是三位数,且这两个三位数的数字分别是0,1,2,3,4,5。设计一个算法用蛮力法求鸡和兔各有多少只,?它们的脚数各是多少
from itertools import permutations feet = permutations([0, 1, 2, 3, 4, 5], 3) feet = list(set([int(''.join([str(j) for j in i])) for i in feet if len(i) == 3 and i[0] != 0])) for chicken in feet: for rabbit in feet: if chicken*2 == rabbit and (chicken%2 == 0): if len(set(str(chicken))^set(str(rabbit))) == 6: print(f"雞 {chicken>>1} 只,{chicken} 只腳\n兔 {rabbit>>2} 只,{rabbit } 只腳")
雞 76 只,152 只腳 兔 76 只,304 只腳
[此贴子已经被作者于2021-8-29 00:25编辑过]
[此贴子已经被作者于2021-9-9 16:55编辑过]
for chick in range(102,255,2): rabbit=2*chick if all((str(chick)+str(rabbit)).count(d) for d in "012345"): print(f"鸡 {chick//2} 只,{chick} 只脚;兔 {rabbit//4} 只,{rabbit} 只脚")
[此贴子已经被作者于2022-12-17 22:13编辑过]