結果

問題 No.518 ローマ数字の和
ユーザー teketeke5000teketeke5000
提出日時 2017-08-21 14:02:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,432 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 178,140 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 01:35:13
合計ジャッジ時間 2,037 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

unordered_map<char, int> ri;
unordered_map<int, char> ir;

void setRome() {
    ri['I'] = 1;
    ri['V'] = 5;
    ri['X'] = 10;
    ri['L'] = 50;
    ri['C'] = 100;
    ri['D'] = 500;
    ri['M'] = 1000;
    for (auto itr = ri.begin(); itr != ri.end(); ++itr) ir[itr->second] = itr->first;
}

int rtoi(string s) {
    int res = 0;
    for (int i=0; i<s.size(); i++) {
        if (ri[s[i]] < ri[s[i+1]]) res -= ri[s[i]];
        else res += ri[s[i]];
    }
    return res;
}

string itor(int num) {
    if (num >= 4000) return "ERROR";
    string res;
    int sta = 1000;
    int n;
    while(num != 0) {
        n = num / sta;
        if (n % 5 == 4) {
            res.push_back(ir[sta]);
            num += sta;
            n++;
        }
        if (n==10) {
            res.push_back(ir[sta*10]);
            num -= sta*10;
            n -= 10;
        }
        if (n >= 5) {
            res.push_back(ir[sta*5]);
            num -= sta*5;
            n -= 5;
        }
        while(n != 0) {
            res.push_back(ir[sta]);
            num -= sta;
            n--;
        }
        sta /= 10;
    }
    return res;
}

int main(void) {
    int ans = 0;
    setRome();
    int N;
    cin >> N;
    vector<string> R(N);
    for (int i=0; i<N; i++) cin >> R[i];
    for (int i=0; i<N; i++) ans += rtoi(R[i]);
    cout << itor(ans) << endl;
    return 0;
}
0