问题:
算法:动态规划
代码:
'''
钢条切割问题
'''
p={1:1,2:5,3:8,4:9,5:10,6:17,7:17,8:20,9:24,10:30}
#递归
def f1(n):
return max([p.get(n,0)]+[f1(i)+f1(n-i) for i in range(1,n//2+1)])
#自底向上
def f2(n):
result=[0]
for i in range(1,n+1):
result.append(max([p.get(i,0)]+[result[j]+result[i-j] for j in range(1,i//2+1)]))
return result[-1]
print(f2(50))