【发布时间】:2016-03-26 11:46:24
【问题描述】:
我正在尝试了解在 stackflow 中发布的 question。
我试图使用作为答案之一给出的递归逻辑来计算 NFL 分数
score = 10
points = [7, 3, 2]
names = {7: "touchdown", 3: "field goals", 2 : "safeties"}
def count_combs(left, i, comb, add):
if add: comb.append(add)
if left == 0 or (i+1) == len(points):
if (i+1) == len(points) and left > 0:
comb.append( (left, points[i]) )
i += 1
while i < len(points):
comb.append( (0, points[i]) )
i += 1
print " ".join("%d %s" % (n,names[c]) for (n,c) in comb)
return 1
cur = points[i]
return sum(count_combs(left-x*cur, i+1, comb[:], (x,cur)) for x in range(0, int(left/cur)+1))
print count_combs(score, 0, [], None)
出来的值不正确。
0 touchdown 0 field goals 10 safeties
必须是 5 个安全而不是 10 个。
【问题讨论】:
标签: python python-2.7