2,763
社区成员
发帖
与我相关
我的任务
分享
class Array
def make_array
arr = []
self.each{|i| arr.insert -1, [i]}
arr
end
def make_array2(e)
self.each{|i| i.insert -1, e}
end
end
def combine(array, size)
return [ array ] if array.size == size
return array.make_array if size == 1
last = array[-1]
left = array - [ last ]
a = combine(left, size - 1).make_array2(last)
b = combine(left, size)
return a + b
end
p combine(%w{ a b c d e}, 4)
p combine(%w{ a b c d e}, 3)
p combine(%w{ a b c d e}, 2)
p combine(%w{ a b c d e}, 1)