Last active
July 2, 2016 08:53
-
-
Save shakemurasan/0b0c2dc54d52b082e0fa940a82f60079 to your computer and use it in GitHub Desktop.
プログラマ脳を鍛える数学パズル:Q04
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
# -- 処理時間計測の自作ライブラリ -- | |
require './process_measure.rb' | |
# ------------------------------ | |
def cut_bar(bar, m) | |
t = m | |
rt_bar = bar.dup | |
bar.each_with_index do |b, i| | |
if b > 1 | |
t -= 1 | |
ca = b / 2 | |
cb = b - ca | |
rt_bar[i] = ca | |
rt_bar.push(cb) | |
end | |
break if t == 0 | |
end | |
rt_bar | |
end | |
def chokkin(n, m) | |
bar = [n] | |
cut_times = 0 | |
while | |
bar = cut_bar(bar, m) | |
cut_times += 1 | |
puts "cut_times #{cut_times}:#{bar.inspect}" | |
break if bar.all? { |b| b == 1 } | |
end | |
end | |
measure_do { chokkin(20, 3) } | |
measure_do { chokkin(100, 5) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment