結果

問題 No.7 プライムナンバーゲーム
ユーザー AIZAWA MasahiroAIZAWA Masahiro
提出日時 2017-03-01 22:32:58
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,407 ms / 5,000 ms
コード長 790 bytes
コンパイル時間 41 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 14,976 KB
最終ジャッジ日時 2024-04-09 04:11:42
合計ジャッジ時間 11,064 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
12,160 KB
testcase_01 AC 83 ms
12,288 KB
testcase_02 AC 1,407 ms
14,848 KB
testcase_03 AC 174 ms
13,184 KB
testcase_04 AC 108 ms
12,416 KB
testcase_05 AC 108 ms
12,672 KB
testcase_06 AC 470 ms
13,952 KB
testcase_07 AC 346 ms
13,696 KB
testcase_08 AC 203 ms
13,440 KB
testcase_09 AC 674 ms
14,720 KB
testcase_10 AC 83 ms
12,288 KB
testcase_11 AC 349 ms
13,568 KB
testcase_12 AC 1,031 ms
14,336 KB
testcase_13 AC 1,092 ms
14,336 KB
testcase_14 AC 1,407 ms
14,976 KB
testcase_15 AC 1,336 ms
14,720 KB
testcase_16 AC 1,238 ms
14,848 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i

def prime?(n)
  root_n = Math.sqrt(n).to_i
  2.upto(n).each do |i|
    break if i > root_n
    return false if n % i == 0
  end
  true
end

def calc(target_number, n, primes, game)
  target_primes = primes.select {|prime| prime < target_number}
  game[target_number] = target_primes.any? do |prime|
    minus_number = target_number - prime
    next false if [0, 1].include?(minus_number)
    !game[minus_number]
  end
  game
end

def play_game(n)
  case n
  when *[0, 1, 2, 3, 4]
    false
  when 5
    true
  else
    primes = 2.upto(n).select do |i|
      prime?(i)
    end
    game = [false, false, false, false, false, true]
    2.upto(n).each do |i|
      game = calc(i, n, primes, game)
    end
    game[n]
  end
end

result = play_game(n)
puts result ? 'Win' : 'Lose'
0