結果

問題 No.518 ローマ数字の和
ユーザー koba-e964koba-e964
提出日時 2017-07-22 21:21:20
言語 Ruby
(3.3.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 970 bytes
コンパイル時間 40 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-04-17 15:57:37
合計ジャッジ時間 2,439 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

def one_level(dig, pat)
  if dig == 9
    return pat[0] + pat[2]
  end
  if dig == 4
    return pat[0] + pat[1]
  end
  if dig >= 5
    return pat[1] + pat[0] * (dig - 5)
  end
  return pat[0] * dig
end

def number_to_roman(num)
  ret = 'M' * (num / 1000)
  num -= num / 1000 * 1000
  for v, pat in [[100, 'CDM'], [10, 'XLC'], [1, 'IVX']]
    dig = num / v
    num -= dig * v
    ret += one_level(dig, pat)
  end
  ret
end

def roman_to_number(str)
  val = 0
  dict = {'IV' => -2, 'IX' => -2, 'XL' => -20, 'XC' => -20, 'CD' => -200,
          'CM' => -200}
  for pat, diff in dict
    if str.include?(pat)
      val += diff
    end
  end
  dict = {'I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100,
          'D' => 500, 'M' => 1000}
  for letter, diff in dict
    val += diff * str.count(letter)
  end
  val
end

_ = gets.to_i
r = gets.chomp.split
sum = 0
for v in r
  sum += roman_to_number(v)
end
if sum >= 4000
  puts "ERROR"
else
  puts number_to_roman(sum)
end
0