結果

問題 No.518 ローマ数字の和
ユーザー 0w10w1
提出日時 2017-08-09 11:37:03
言語 Ruby
(3.3.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,311 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-04-20 08:30:54
合計ジャッジ時間 2,589 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 77 ms
12,160 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 73 ms
12,032 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 73 ms
12,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
R = gets.chomp.split.map

def decode s
  v, ptr = 0, 0
  val = { ?I => 1, ?V => 5, ?X => 10, ?L => 50, ?C => 100, ?D => 500, ?M => 1000 }
  until ptr == s.size
    while s.size - ptr >= 2 && val[s[ptr]] < val[s[ptr + 1]]
      v += val[s[ptr + 1]] - val[s[ptr]]
      ptr += 2
    end
    case s[ptr]
    when ?M
      (ptr += 1; v += 1000) while s[ptr] == ?M
    when ?D
      (ptr += 1; v += 500) while s[ptr] == ?D
    when ?C
      (ptr += 1; v += 100) while s[ptr] == ?C
    when ?L
      (ptr += 1; v += 50) while s[ptr] == ?L
    when ?X
      (ptr += 1; v += 10) while s[ptr] == ?X
    when ?V
      (ptr += 1; v += 5) while s[ptr] == ?V
    when ?I
      (ptr += 1; v += 1) while s[ptr] == ?I
    end
  end
  v
end

def encode v
  s = ""
  (s << ?M; v -= 1000) until v < 1000
  (s << "CM"; v -= 900) until v < 900
  (s << ?D; v -= 500) until v < 500
  (s << "CD"; v -= 400) until v < 400
  (s << ?C; v -= 100) until v < 100
  (s << "XC"; v -= 90) until v < 90
  (s << ?L; v -= 50) until v < 50
  (s << "XL"; v -= 40) until v < 40
  (s << ?X; v -= 10) until v < 10
  (s << "IX"; v -= 9) until v < 9
  (s << ?V; v -= 5) until v < 5
  (s << "IV"; v -= 4) until v < 4
  (s << ?I; v -= 1) until v < 1
  s
end

tot = R.inject(0) { |a, b| a + decode(b) }
puts tot > 3999 ? "ERROR" : encode(tot)
0