結果

問題 No.3 ビットすごろく
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2014-12-11 07:19:48
言語 Ruby
(3.3.0)
結果
AC  
実行時間 509 ms / 5,000 ms
コード長 866 bytes
コンパイル時間 604 ms
コンパイル使用メモリ 11,292 KB
実行使用メモリ 15,464 KB
最終ジャッジ日時 2023-09-13 22:57:52
合計ジャッジ時間 9,729 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,156 KB
testcase_01 AC 84 ms
15,124 KB
testcase_02 AC 84 ms
15,256 KB
testcase_03 AC 108 ms
15,156 KB
testcase_04 AC 85 ms
15,300 KB
testcase_05 AC 204 ms
15,116 KB
testcase_06 AC 113 ms
15,032 KB
testcase_07 AC 91 ms
15,120 KB
testcase_08 AC 158 ms
15,308 KB
testcase_09 AC 285 ms
15,184 KB
testcase_10 AC 371 ms
15,172 KB
testcase_11 AC 253 ms
15,064 KB
testcase_12 AC 198 ms
15,264 KB
testcase_13 AC 100 ms
15,180 KB
testcase_14 AC 354 ms
15,096 KB
testcase_15 AC 496 ms
15,324 KB
testcase_16 AC 439 ms
15,120 KB
testcase_17 AC 486 ms
15,332 KB
testcase_18 AC 97 ms
15,032 KB
testcase_19 AC 508 ms
15,464 KB
testcase_20 AC 86 ms
15,096 KB
testcase_21 AC 82 ms
15,104 KB
testcase_22 AC 361 ms
15,356 KB
testcase_23 AC 507 ms
15,120 KB
testcase_24 AC 509 ms
15,204 KB
testcase_25 AC 496 ms
15,256 KB
testcase_26 AC 83 ms
15,236 KB
testcase_27 AC 104 ms
15,132 KB
testcase_28 AC 427 ms
15,168 KB
testcase_29 AC 258 ms
15,308 KB
testcase_30 AC 81 ms
15,256 KB
testcase_31 AC 81 ms
15,228 KB
testcase_32 AC 231 ms
15,216 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class BitSugoroku
  def initialize(n)
    @n = n
  end

  def saitan
    if @n == 1
      return 1
    end
    i = 1
    kazoeta = [1]
    list = [1]
    while list.size > 0
      i += 1
      next_list = []
      list.each do |x|
        bits = num_bit(x)
        add_n = x + bits
        del_n = x - bits
        if add_n == @n
          return i
        elsif (add_n < @n) && (! kazoeta.include?(add_n))
          kazoeta << add_n
          next_list << add_n
        end
        if (! kazoeta.include?(del_n))
          kazoeta << del_n
          next_list << del_n
        end
      end
      list = next_list
    end
    return -1
  end

  def num_bit(x)
    count = 0
    while x > 0
      count += x & 1
      x = x >> 1
    end
    return count
  end
end

n = gets.to_i
bs = BitSugoroku.new(n)
puts bs.saitan
0