想通过递归执行以下操作,以便我可以改变“for”循环的数量:
n = 5
out = []
for i in range(n):
for j in range(i,n):
for k in range(j,n):
out.append([i,j,k])
返回
out = [[0 0 0]
[0 0 1]
[0 0 2]
[0 0 3]
[0 0 4]
[0 1 1]
[0 1 2]
[0 1 3]
[0 1 4]
[0 2 2]
[0 2 3]
[0 2 4]
[0 3 3]
[0 3 4]
[0 4 4]
[1 1 1]
[1 1 2]
[1 1 3]
[1 1 4]
[1 2 2]...]
例如
def Recurse(n, p):
# where p is the number of for loops
some magic recursion
return out
我看过其他一些递归问题,但很难找到解决方案。