我试图在给定的总质量的基础上,生产出一系列可能的树。第一层质量为1的所有树,对于下面的所有后续层,它们要么保持前一层的质量,要么增加1。(暂时不包括茎)我想递归会把我引向光明,但是我似乎有一些问题,产生更多的1的细分支,一直到底部。你知道吗
第一次通过后,错误显示:
File "test3.py", line 21, in <module>
for perm in tree(56):
TypeError: 'NoneType' object is not iterable
我不确定它要迭代到哪里。我对递归几乎没有经验,而且只在Prolog中。这是我的程序:
def tree(total):
tree = [1]
current = 1
def build_up(total,current):
if total == 1:
tree.append(1)
print(tree)
return 1
elif total <= 0:
return 0
else:
for i in range(current,current+1):
tree.append(i)
current = i
total -= i
build_up(total,current)
build_up(total-1,current)
for perm in tree(56):
print(perm)
目前没有回答
相关问题 更多 >
编程相关推荐