python数学解题_python 解数学题!求帮助

[Python] 纯文本查看 复制代码#获取某个位置之后3个的火柴

def Get3Plu(aDict: dict, index: int) -> int:

count = 0

for i in range(index + 1, 11):

if i in aDict.keys():

count += 1

if count == 3:

return i

return 0

#获取某个位置之前3个的火柴

def Get3Min(aDict: dict, index: int) -> int:

count = 0

for i in range(index - 1, 0, -1):

if i in aDict.keys():

count += 1

if count == 3:

return i

return 0

#获取所有移动的可能性

def GetAllPosible(aDict: dict) -> list:

result = []

for i in aDict.keys():

get3Plu = Get3Plu(aDict, i)

get3Min = Get3Min(aDict, i)

if get3Min != 0:

if not aDict[get3Min]:

result.append((i, get3Min))

if get3Plu != 0:

if not aDict[get3Plu]:

result.append((i, get3Plu))

return result

#处理一种可能性,火柴从opt[0]移动到opt[1]

def doStep(aDict: dict, opt: (int, int)):

newDict = aDict.copy()

newDict[opt[1]] = True

newDict.pop(opt[0])

return newDict

#做一大步处理, 由一种现状, 推测出所有可能性, 并返回所有处理结果

def goStep(aDict: dict):

all_result = []

temp_opt = GetAllPosible(aDict)

if temp_opt.__len__() > 0:

for opt in temp_opt:

temp_dic = doStep(aDict, opt)

all_result.append(temp_dic)

return all_result

if __name__ == '__main__':

list_All = []

# 数字表示位置, False为1根, True为2根, 没有(pop)为0根

orgin = {1: False, 2: False, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False, 9: False, 10: False}

# 一定是5步 才OK

# 第一步

list_step = goStep(orgin)

# 剩下的4步

for i in range(4):

list_step_temp = []

for dic in list_step:

list_step_temp.extend(goStep(dic))

list_step = list_step_temp

print("共遍历了", len(list_step), "种方法")

result_list = []

for item in list_step:

if list(item.values()) == [True, True, True, True, True]:

result_list.append(item)

print(len(result_list), "种方法成功了")

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值