题解 | #百钱买百鸡问题#
百钱买百鸡问题
https://www.nowcoder.com/practice/74c493f094304ea2bda37d0dc40dc85b
#先买99只鸡雏,剩67元,判断用67元能否买1只鸡母或者鸡翁
#从67元逐渐增加到100元(step=1),需要购买的鸡母或者鸡翁数量从1只逐渐增加到100只(step=3)。
#m元,分配给n只鸡,其中x只鸡母和(n-x)只鸡翁,使得m = 3*x + 5*(n-x),因此2x = (5n - m),其中n>=x>=0,且m,n,x都是整数。
m = 67
n = 1
i = 0
a = []
while i<=33:
m = 67 + i
n = 1 + 3 * i
x = (5*n - m)/2
if ((n>x) or (abs(n-x)<1e-6)) and ((x>0) or (abs(x)<1e-6)) and (round(x, 6).is_integer()):
a.append((int(n-x), int(x), 100-n))
i += 1
for i in a[::-1]:
print(*i)
