全组合的递归实现(ruby)
2016-12-18 21:18
288 查看
着急用所以直接扒了一个C++算法翻译成了ruby…暂记
1 nodes = ["node1", "node2", "node3", "node4", "node5", "node6"] 2 3 def combine_array(arr, start, index, count, num ,len, output) 4 for i in (start..len-1) 5 index[count-1] = i 6 if count-1 == 0 then 7 out = [] 8 for j in (0..num-1) 9 out << arr[index[j]] 10 end 11 #puts "#{out}" 12 #puts "------------" 13 output << out 14 else 15 combine_array(arr, i+1, index, count-1, num, len, output) 16 end 17 end 18 end 19 20 for i in (1..6) 21 outs = [] 22 combine_array(nodes, 0, index, i, i, len, outs) 23 p outs 24 puts "------------" 25 end
相关文章推荐
- 递归实现数字排列组合
- 递归实现数字的组合(C++)
- 组合非递归实现
- 递归实现排列组合
- 从数组中取出n个元素的所有组合(递归实现)
- 递归实现无重复组合
- 排列与组合的Java递归实现 (参考)
- java 递归 实现数字的排列组合
- 试题:组合的模板非递归实现
- 字符串的组合问题 递归和非递归实现
- 递归实现数字的组合(C++)
- 递归实现全排列和组合
- 字符串的全组合非递归实现
- 从数组中取出n个元素的所有组合(递归实现)
- Java 非递归实现的排列组合中的平均分组问题
- 组合函数递归和非递归实现
- 用递归实现从M个不同字符中选取N个字符的所有组合
- 列出数组中取出的数的全部组合(用递归实现)
- 排列组合算法的递归实现
- 使用 Lambda 表达式编写递归三:实现 Y 组合子