Last active
December 23, 2015 19:39
-
-
Save tohka/6683731 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/local/bin/ruby | |
# CodeIQ のナムドット問題 | |
# https://codeiq.jp/ace/yuki_hiroshi/q468 | |
# 12345 => 00000 | |
# 1234.5 => 00001 | |
# 1235.4 => 00010 | |
# 123.45 => 00011 | |
# 123.4.5 => 00012 | |
# 1245.3 => 00100 | |
# 124.35 => 00101 | |
# 124.3.5 => 00102 | |
# 125.34 => 00110 | |
# 12.345 => 00111 | |
# 12.34.5 => 00112 | |
# | |
# 上記のように各数字がドット区切りでどの位置にあるかを表すと | |
# N 進数のように表記される。ここで桁があがる N は動的に変化する。 | |
# | |
# 対象の桁より左方の桁のうち、最大の数が 1 のときは 2 まで | |
# 取りうることができるが、 3 を取ることができない。 | |
# そのときは桁をあげる必要がある。 | |
# それぞれ、 1, 2, 3, 4, 5 の数字がどの位置にあるかを管理する配列 | |
# "123.4.5" であれば [0, 0, 0, 1, 2] とする | |
counters = [0, 0, 0, 0, 0] | |
52.times do | |
blocks = ["", "", "", "", ""] | |
# counters = [0, 0, 0, 1, 2] であれば | |
# blocks = ["123", "4", "5", "", ""] となるような配列 | |
5.times do |i| | |
blocks[counters[i]] << "#{i+1}" | |
end | |
# 出力する | |
blocks.delete("") | |
puts blocks.join(".") | |
# 1 加算する。必要あれば桁上げを行う | |
counters[4] += 1 | |
4.downto(1) do |i| | |
if counters[i] > counters[0, i].max+1 | |
counters[i-1] += 1 | |
counters[i] = 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment