MITx: 6.00.1x Python Quiz

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

 
(本题共有10分)

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 ab, and c such that

6a+9b+20c=n

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值