0
我想了解question張貼在stackflow中。遞歸邏輯計算硬幣變化/足球比分
我試圖用給出的答案之一來計算遞歸的邏輯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.
完美。謝謝 – paddu