用n元钱买橙子,6元/8元一包,不零卖。设计函数返回最少袋数或-1

79 阅读1分钟

def test(n):
all = []
for x in range(n/6+1):
for y in range(n/8+1):
if 6*x+8*y == n:
all.append(x+y)

try:
    return sorted(all)[0]
except:
    return -1

print test(20)