Example: There are 100 text money, to buy 100 chickens, Rooster 5 A, hen 3 article A, chick three only one article, ask the Rooster hen how many chickens each?
Method One:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 forAinchRange (100//5+1):4 forBinchRange (100//3+1):5 forCinchRange (0, 100*3+1, 3):6 ifA + B + C ==100 7 andA*5 + b*3 + (C//3) * = = 100:8 Print('gj:%3d mj:%3d xj:%3d'% (A, B, c))
Execution Result:
gj:0 mj:25 xj:75
Gj:4 mj:18 xj:78
Gj:8 mj:11 xj:81
Gj:12 Mj:4 xj:84
Method Two:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #x + y + z =4 #5x + 3y + Z/3 =5 forXinchRange (1, 20):6 forYinchRange (1, 33):7z = 100-y-x8 ifZ% 3 = = 0 and5 * x + 3 * y + Z/3 = 100:9 Print('Rooster:'X'only','Hens:'Y'only','Chick:'Z'only')
Execution Result:
Rooster: 4 Hens: 18 Chickens: 78
Rooster: 8 Hens: 11 Chickens: 81
Rooster: 12 Hens: 4 Chickens: 84
python--hundred money hundred chicken problem