質問

私はこれでしばらく遊んでいますが、明らかな解決策が見当たりません。 XinY_Go関数から再帰を削除したい。

def XinY_Go(x,y,index,slots):
   if (y - index) == 1:
      slots[index] = x
      print slots
      slots[index] = 0
      return
   for i in range(x+1):
      slots[index] = x-i
      XinY_Go(x-(x-i), y, index + 1, slots)

def XinY(x,y):
   return XinY_Go(x,y,0,[0] * y)

この関数は、X個のビー玉をYスロットに入れる方法の数を計算しています。出力例を次に示します。

 >>> xy.XinY(1,2)
 [1, 0]
 [0, 1]
 >>> xy.XinY(2,3)
 [2, 0, 0]
 [1, 1, 0]
 [1, 0, 1]
 [0, 2, 0]
 [0, 1, 1]
 [0, 0, 2]
役に立ちましたか?
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top