54张牌两人轮流抽'1-4'张,先抽几张,才能确保最后一张一定被自己抽到

内容如标题,原题是小学四年级的一个动脑筋。


稍微改动一下,比如总数有N张牌,两人轮流抽'1-m'张,先抽几张,才能确保最后一张一定被自己或对手抽到。

python写的,代码如下:

def get_answer():
    total = input('please input the number of total card:\n')
    every_card = input('please input the number of everyone should take:\n')
    last_status = input("please define take the last one win or lose:\n,win input '1', lose input '0'\n")
    # last_status = True

    loop_num = int(math.ceil(total / every_card))
    for j in range(1, every_card + 1):
        for i in range(loop_num):
            if last_status:
                if total == j + (i * (every_card + 1)):
                    plan_dic = dict()
                    plan_dic['first_take'] = j
                    plan_dic['held_num'] = every_card + 1
                    plan_dic['times'] = i
                    print plan_dic
            else:
                if total == j + ((i - 1) * (every_card + 1)) + every_card:
                    plan_dic = dict()
                    plan_dic['first_take'] = j
                    plan_dic['held_num'] = every_card + 1
                    plan_dic['times'] = i
                    print plan_dic

其实还可以再改动一下,比如N个人轮流抽,或许这样没有解的情况会比较多了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值