用python语言实现“连续邮资”问题的回溯算法,怎么求?
求解答…
Max = 4
Prices = [0,1,3,11,15,32] # 单价列表
def Traceback(count):
global tmp,flag,Result
if count==Max:
if tmp == Result+1:
flag = True
return
for i in range(len(Prices)):
tmp += Prices[i]
if tmp <= Result+1:
Traceback(count+1)
tmp -= Prices[i]
Result = 0
tmp = 0
while True:
flag = False
Traceback(0)
if flag:
Result += 1
else:
break
print(Result)