結果

問題 No.518 ローマ数字の和
ユーザー むらためむらため
提出日時 2019-01-26 01:26:07
言語 Nim
(2.0.2)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,528 bytes
コンパイル時間 3,224 ms
コンパイル使用メモリ 68,428 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 02:53:22
合計ジャッジ時間 4,651 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,strutils,math
template times*(n:int,body) = (for _ in 0..<n: body)

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord
proc parseRoman(S:string): int =
  return S.replace("CM"," 900 ").replace("CD"," 400 ")
   .replace("XC"," 90 ").replace("XL"," 40 ")
   .replace("IX"," 9" ).replace("IV"," 4 ")
   .replace("M"," 1000 ").replace("D"," 500 ")
   .replace("C"," 100 ").replace("L"," 50 ")
   .replace("X"," 10 ").replace("V"," 5 ")
   .replace("I"," 1 ").replace("  "," ").strip().split().map(parseInt).sum()

proc toRoman(n:int):string =
  if n < 10:
    if n == 4 : return "IV"
    if n == 9 : return "IX"
    if n < 4: return "I".repeat(n)
    return "V" & "I".repeat(n-5)
  if n < 100:
    let left = (n mod 10).toRoman()
    let my = n div 10
    if my == 4 : return "XL" & left
    if my == 9 : return "XC" & left
    if my < 4 : return "X".repeat(my) & left
    return "L" & "X".repeat(my-5) & left
  if n < 1000:
    let left = (n mod 100).toRoman()
    let my = n div 100
    if my == 4 : return "CD" & left
    if my == 9 : return "CM" & left
    if my < 4 : return "C".repeat(my) & left
    return "D" & "C".repeat(my-5) & left
  if n < 4000:
    let left = (n mod 1000).toRoman()
    let my = n div 1000
    return "M".repeat(my) & left
  return "ERROR"
let n = scan()
let r = stdin.readLine.split().map(parseRoman).sum()
echo r.toRoman()
0