Created
September 24, 2013 18:03
-
-
Save whatalnk/6688845 to your computer and use it in GitHub Desktop.
CodeIQ 結城浩さん出題「ナムドット問題」
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# steps: 繰り返し回数 | |
def numDot(steps) | |
lv = 1 | |
# シフト前の配列、最初は1 | |
ary = [[lv.to_s]] | |
return ary if steps == 1 | |
# stepsが2以上の場合、iをシフトさせていく | |
for i in 2..steps do | |
# 結果の配列 | |
res = Array.new | |
# 各要素の末尾にシフトする数字を結合 | |
ary.each do |x| | |
for j in 0..x.size-1 do | |
v = x.dup() | |
v[j] = v[j] + i.to_s | |
res << v | |
end | |
# 配列の末尾にシフトする数字を追加 | |
res << x.push(i.to_s) | |
end | |
ary = res.dup() | |
end | |
return res | |
end | |
# ドットでつないで結果を出力 | |
numDot(5).each {|x| puts x.join(".")} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment