close

八取四不重覆,並且排列順序列入考慮(permutation)

#!/usr/bin/env ruby

require 'permutation'

str = "abcdefgh"
nums = 4  #取的個數
perm = Permutation.for("#{str}")
arr = Array.new
arr = perm.map { |p| p.project[0..(nums-1)] }.sort.uniq

arr.each do |e|
 puts e
end

puts arr.uniq.size

 

 

 

八取四不重覆,並且排列順序不列入考慮(comprehension)

#!/usr/bin/env ruby

require 'permutation'

str = "12345678"
nums = 4  #取的個數
perm = Permutation.for("#{str}")
arr = Array.new
arr = perm.map { |p| p.project[0..(nums-1)].split(//).sort.join }.sort.uniq

arr.each do |e|
 puts e
end

puts arr.size


 

 

參考:

http://dufu.math.ncu.edu.tw/calculus/calculus_eng/node208.html

arrow
arrow
    全站熱搜

    igogo 發表在 痞客邦 留言(0) 人氣()