結果
| 問題 | 
                            No.518 ローマ数字の和
                             | 
                    
| コンテスト | |
| ユーザー | 
                             teketeke5000
                         | 
                    
| 提出日時 | 2017-08-21 14:00:07 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,484 bytes | 
| コンパイル時間 | 1,421 ms | 
| コンパイル使用メモリ | 177,604 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-15 00:48:46 | 
| 合計ジャッジ時間 | 2,074 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
ソースコード
#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;
}
            
            
            
        
            
teketeke5000