【发布时间】:2021-08-17 22:10:51
【问题描述】:
我试图找出构成幂和的所有数字组合。下面是我的代码,它在找到第一个组合后返回。
`代码:
def powerSum(targetSum, N):
def helper(x,n,c):
if pow(c,n)==x:
return [c]
if pow(c,n)>x:
return None
l = helper(x,n,c+1)
r = helper(x-pow(c,n),n,c+1)
if l!=None or r!=None:
if l==None:
return r+[c]
else:
return l
return helper(targetSum,N,1)
print(powerSum(100,2))
有人可以帮我返回所有可能的组合吗 例子: 如果输入是 targetSum =100 并且 N=2 输出应该是三个可能的组合列表的列表 =[[10],[6,8][1,3,4,5,7]] 我的输出只有 [10]
【问题讨论】:
标签: python recursion dynamic-programming