内容如标题,原题是小学四年级的一个动脑筋。
稍微改动一下,比如总数有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个人轮流抽,或许这样没有解的情况会比较多了。