动态规划01--切钢条(1)

# -*- coding: utf-8 -*-
import random

def getp(n):
    '''
    获取一个长度为n的价格表
    '''
    p = [0 for i in range(n)]
    for i in range(n):
        p[i] = random.randint(1, n)
    return p

def cut_rod(p, n):
    if n==0:
        return 0
    else:
        q = 0
        for i in range(1, n+1):
            #从1开始切割, 切刀最后一个结束
            q = max(q, p[i-1] + cut_rod(p, n-i))
        return q

n = 10
p = getp(n)
print(p)
q = cut_rod(p, n)
print(q)

你可能感兴趣的:(数据结构与算法)