結果

問題 No.518 ローマ数字の和
ユーザー treeonetreeone
提出日時 2019-05-20 15:29:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,228 bytes
コンパイル時間 3,092 ms
コンパイル使用メモリ 202,952 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 08:05:53
合計ジャッジ時間 3,225 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
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 WA -
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 WA -
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 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[7] = {1, 5, 10, 50, 100, 500, 1000};
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 = str.size() - 1; i >= 0; i--){
        int tmp = n / d[i];
        n -= d[i] * tmp;
        if(tmp == 4){
            res += str[i];
            res += str[i + 1];
        }else{
            for(int j = 0; j < tmp; j++) res += str[i];
        }
    }
    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