我被要求做以下事情:如果有可能购买 x、x+1、...、x+5 套 McNuggets,对于一些 x,那么有可能购买任意数量的 McNuggets >= x,给定McNuggets 有 6 包、9 包和 20 包。编写一个迭代程序,找出不能以确切数量购买的最大数量的 McNuggets。
这是我想出的代码,但它陷入了无限循环:
count = 0
n = 1
while count < 6:
six_consequtive = True
for a in range(n):
for b in range(n):
for c in range(n):
if 6*a + 9*b + 20*c == n:
six_consequtive = False
if six_consequtive:
count += 1
else:
count = 0
n += 1
print("Largest number of McNuggets that cannot be bought in exact quantity: %d." % (n - 5))
非常感谢!