Skip to content

Instantly share code, notes, and snippets.

@kaochenlong
Created April 13, 2012 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaochenlong/2375055 to your computer and use it in GitHub Desktop.
Save kaochenlong/2375055 to your computer and use it in GitHub Desktop.
def get_combinations(n, m)
combinations = []
(0..n-1).to_a.combination(m) do |c|
array = Array.new(n, 0)
c.each { |i| array[i] = 1 }
combinations << array.join
end
combinations
end
# 5取3
p get_combinations 5, 3
# 得到結果 ["11100", "11010", "11001", "10110", "10101", "10011", "01110", "01101", "01011", "00111"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment