MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
#code to calculate possible McNugget combinations up to n = 55 nuggets
n=0
a=0
b=0
c=0
while n < 55:
if 6*a + 9*b + 20*c < n:
a = a + 1
elif 6*a + 9*b + 20*c > n:
a = 0
b = b + 1
if 9*b > n:
b = 0
c = c + 1
if 20*c > n:
c = 0
n = n + 1
elif 6*a + 9*b + 20*c == n:
print 'For', n, 'McNuggets, a =', a, ', b =', b, ', c =', c
a = a + 1
#code to calculate the largest quantity of McNuggets which cannot be obtained using the three sizes small, medium, and large
bestSoFar = 0 # variable that keeps track of largest number
# of McNuggets that cannot be bought in exact quantity
packages = (6,9,20) # variable that contains package sizes
(small, medium, large) = packages
for n in range(1, 200): # only search for solutions up to size 200
a = 0
b = 0
c = 0
while large*c < n:
if small*a + medium*b + large*c < n:
a = a + 1
elif small*a + medium*b + large*c > n:
a = 0
b = b + 1
if medium*b > n:
b = 0
c = c + 1
if small*a + medium*b + large*c == n:
break
elif large*c > n:
print n, "McNuggets cannot be bought in exact quantity."
bestSoFar = n
print "Largest number of McNuggets that cannot be bought in exact quantity:", bestSoFar
kpmoore
1 year ago