Comment detail
自然数の分割 (Nested Flatten)divNat(n,m)で、n>9の場合エラーになります。 考え直さねば。
分解後の数字が2桁の場合の処理を簡潔に書く方法を思いつかなかったのと、スピードアップのために方針変更。 1桁目はnから0、2桁目は(n-1桁目)から0、...としました。
1 2 3 4 5 6 7 8 9 10 11 12 | def divNat(n, m, acc = [])
if m == 1
p acc << n
return
end
n.downto(0) {|i|
w = acc[0..-1]
divNat(n-i,m-1, w << i)
}
end
divNat(5,3)
|



kenaxt #4347() [ Ruby ] Rating3/3=1.00
Rating3/3=1.00-0+
1 reply [ reply ]