1. 程式人生 > >python練習題之雞兔同籠

python練習題之雞兔同籠

for x in range(1,100):
    for y in range(1,100):
        z = 100 -x-y
        if (5*x+3*y+z/3==100) and z%3==0:
            print(x,y,z)



cock_price,hen_price,chick_price=5,3,1.0/3
cock_MaxNum,hen_MaxNum,chick_MaxNum=range(100/cock_price)[1:],range(100/hen_price)[1:],range(int(100/chick_price))[1:]
items=[(cock,hen,chick)for cock in cock_MaxNum for hen in  hen_MaxNum[1:] for chick in chick_MaxNum[1:]
        if int(cock*cock_price+hen*hen_price+chick*chick_price)==100 and chick%3==0 and cock+hen+chick==100]

print([v for v in  [(i,j,k) for i in range(21) for j in range(34)  for k in range(101) if i*5+j*3+k==100]])