Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jokester/1143767 to your computer and use it in GitHub Desktop.
Save jokester/1143767 to your computer and use it in GitHub Desktop.
求数组{ (0..N1),{0..N2},...,(0..Nn) }的笛卡尔积
#!/usr/bin/ruby
puts "input N"
i=gets.to_i
m=[1]
1.upto i do |j|
puts "input N#{j}"
k=gets.to_i
m.unshift m[0]*(k+1)
end
0.upto m[0]-1 do |j|
o=[]
1.upto(i) do |k|
o.unshift (j%m[k-1])/m[k]
end
p o
end
@edokeh
Copy link

edokeh commented Feb 13, 2013

Ruby 里面自带了计算笛卡尔积的方法
["a","b"].product ["c","d"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment