結果

問題 No.518 ローマ数字の和
ユーザー teketeke5000teketeke5000
提出日時 2017-08-21 13:59:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,447 bytes
コンパイル時間 1,590 ms
コンパイル使用メモリ 177,660 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-23 01:34:42
合計ジャッジ時間 2,487 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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) {
    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