LoginSignup
0
2

More than 5 years have passed since last update.

オフラインリアルタイムどう書く E16 の実装例

Last updated at Posted at 2017-08-05

問題 : http://nabetani.sakura.ne.jp/hena/orde16nontri/
実装リンク集 : http://qiita.com/Nabetani/items/3525da55601bdf55316e

ruby
def impl( s )
  sb = s.to_s(2)
  pos = ( sb=~/(000|111)/ )
  return s unless pos
  impl(
    case $1
    when "000"
      sb.sub( /000.*/ ){ |m,| "001"+"0"*(m.size-3) }.to_i(2)
    when "111"
      sb.sub( /111.*/ ){ |m,| "1"*m.size }.to_i(2) + 1
    end
    )
end

def solve(src)
  impl( src.to_i+1 ).to_s
end

if $0==__FILE__
  DATA.map{ |line|
    num, src, expected = line.split(/\s+/)
    actual = solve( src )
    okay = expected==actual
    p [ (okay ? "ok" : "**NG**" ), num, src, actual, expected ]
    okay
  }.all?.tap{ |x| p( x ? "everythin is okay" : "something wrong")  }
end

__END__
0 1441  1444
40  1072693248  1227133513

いつも通りテストデータの大半は省略。

000 があったら、その桁以降を 0010000...0 に置換
111 があったら、その桁以降を 1111111...1 に置換して 1 を足して繰り上げる。

この処理を再帰的に行うと悲惨連数非三連数が出来上がるという仕組み。

簡単すぎたかな。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2