結果

問題 No.518 ローマ数字の和
ユーザー treeonetreeone
提出日時 2019-05-20 16:00:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,576 bytes
コンパイル時間 2,695 ms
コンパイル使用メモリ 203,624 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 08:06:01
合計ジャッジ時間 3,180 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
typedef pair<int, int> P;

const int INF = 1e8;
int d[8] = {1, 5, 10, 50, 100, 500, 1000, 5000};
string str = "IVXLCDM";

int change_10(string s){
    int res = 0;
    int now, pre = INF;
    rep(i, 0, s.size()){
        int id = str.find(s[i]);
        now = d[id];
        if(pre < now){
            res += now - pre;
            pre = INF;
        }else{
            if(pre != INF) res += pre;
            pre = now;
        }
    }
    if(pre != INF) res += pre;
    return res;
}

string change_roman(int n){
    string res = "";
    for(int i = 0; i < str.size(); i++){
        int tmp = (n % d[i + 1]) / d[i];
        // cout << n << ' ' << d[i + 1] << ' ' << d[i] << ' ' << tmp << endl;
        if(tmp == 4){
            if((n % d[i + 2]) / d[i] == 9){
                res += str[i + 2];
                res += str[i];
                n -= 9 * d[i];
            }else{
                res += str[i + 1];
                res += str[i];
                n -= tmp * d[i];
            }
        }else{
            for(int j = 0; j < tmp; j++) res += str[i];
            n -= tmp * d[i];
        }
    }
    reverse(res.begin(), res.end());
    return res;
}

int main(){
    int n;
    cin >> n;
    int sum = 0;
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        int tmp = change_10(s);
        sum += tmp;
    }
    if(sum >= 4000){
        cout << "ERROR" << endl;
        return 0;
    }
    string ans = change_roman(sum);
    cout << ans << endl;
}
0