結果

問題 No.518 ローマ数字の和
ユーザー finefine
提出日時 2017-05-28 21:45:29
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 840 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 11,908 KB
実行使用メモリ 16,092 KB
最終ジャッジ日時 2023-10-21 14:07:33
合計ジャッジ時間 5,222 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
16,092 KB
testcase_01 AC 89 ms
16,092 KB
testcase_02 AC 90 ms
16,092 KB
testcase_03 AC 88 ms
16,092 KB
testcase_04 AC 88 ms
16,092 KB
testcase_05 AC 89 ms
16,092 KB
testcase_06 AC 91 ms
16,092 KB
testcase_07 AC 88 ms
16,092 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:38: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #

@h = {?I => 1, ?V => 5, ?X => 10, ?L => 50, ?C => 100, ?D => 500, ?M => 1000}
@h2 = @h.invert

def hoge(r)
    m = r.size
    res = @h[r[m - 1]]
    (m-1).times do |i|
        if @h[r[i]] < @h[r[i + 1]]
            res -= @h[r[i]]
        else
            res += @h[r[i]]
        end
    end
    return res
end

def foo(x)
    return "ERROR" if x >= 4000
    res = ""
    d = 1
    while x > 0
        next if x % 10 == 0
        if x % 10 == 9
            res = @h2[d] + @h2[d * 10] + res
        elsif x % 10 == 4
            res = @h2[d] + @h2[d * 5] + res
        elsif x % 10 > 4
            res = @h2[d * 5] + @h2[d] * (x % 10 - 5) + res
        else
            res = @h2[d] * (x % 10) + res
        end
        x /= 10
        d *= 10
    end
    return res
end

n = gets.to_i
puts foo(gets.chomp.split.map{|r| hoge(r)}.inject(:+))
0