Q:公雞5元一只,母雞3元一只,小雞3只一元,100元剛好買100只雞,公雞母雞小雞必須都要有,請問公雞母雞小雞各幾只?
實現代碼:
for x in range(1,21): for y in range(1,34): z=100-x-y if (z%3==0) and (5*x+y*3+z/3==100): print('公雞:%d只,母雞:%d只,小雞:%d只' %(x,y,z))
運行結果:
公雞:4只,母雞:18只,小雞:78只 公雞:8只,母雞:11只,小雞:81只 公雞:12只,母雞:4只,小雞:84只