代码有一个我找不到的错误。
def cut_rod(p, n):
if (n == 0):
return 0
q = p[n];
for i in range(1,n):
q = max(q, p[i] + cut_rod(p, n - i));
return q;
return;
# your code goes here
p1 = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30];
n1 = len(p1);
print n1;
a = cut_rod(p1,n1);
答案 0 :(得分:0)
您的代码存在的一个问题是,您的列表中不存在定义中的索引n1
。 Python列表索引从0开始,因此长度为3的列表只有索引0-2。
mylist = ["a", "b", "c"]
print(len(mylist))
# 3
print(mylist[3])
# IndexError!
print(mylist[2])
# "c"
而不是:
n1 = len(p1)
试试这个:
n1 = len(p1) - 1