def f(n):
''''''
remi = n % 6
if n % 6 == 0:
return True
else:
for i in range(int(n/6), 0, -1):
remi = remi + 6
if remi % 9 == 0:
return True
else:
remiB = remi % 9
for j in range(int(remi / 9) , 0, -1):
remiB = remiB + 9
if remiB % 20 == 0:
return True
else:
if remiB % 20 % 9 == 0:
return True
return False
在courses上选了MIT的Python课程,昨天第一次测验时间结束。有两道编程题难倒我了。今天,闷了两小时,终于搞出来一个。真是太笨了! 幸亏我不是程序员,仅仅是爱好!
下面是题目:
PROBLEM 7
McDonald’s sells Chicken McNuggets in packages of 6, 9 or 20 McNuggets. Thus, it is possible, for example, to buy exactly 15 McNuggets (with one package of 6 and a second package of 9), but it is not possible to buy exactly 16 McNuggets, since no non- negative integer combination of 6's, 9's and 20's add up to 16. To determine if it is possible to buy exactly n McNuggets, one has to find non-negative integer (can be 0) values of a, b, and c such that
Write a function, called McNuggets
that takes one argument, n
, and returns True if it is possible to buy a combination of 6, 9 and 20 pack units such that the total number of McNuggets equals n
, and otherwise returns False. Hint: use a guess and check approach.
Note: You will only get ten checks. Use these judiciously.