結果

問題 No.518 ローマ数字の和
ユーザー Manuel1024Manuel1024
提出日時 2021-09-20 03:36:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,051 bytes
コンパイル時間 672 ms
コンパイル使用メモリ 72,340 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-15 02:50:11
合計ジャッジ時間 2,098 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
using namespace std;

const string c = "IVXLCDM";
int cn[] = {1, 5, 10, 50, 100, 500, 1000};
const vector<string> dc = {"IV", "IX", "XL", "XC", "CD", "CM"};
int dcn[] = {4, 9, 40, 90, 400, 900};

int dec(const string &r){
    int res = 0;
    for(int i = 0; i < r.length(); i++){
        bool added = false;
        for(int j = 0; j < 6; j++){
            if(r.substr(i, 2) == dc[j]){
                res += dcn[j];
                added = true;
                i++;
            }
        }
        if(added) continue;
        for(int j = 0; j < 7; j++){
            if(r[i] == c[j]){
                res += cn[j];
            }
        }
    }
    return res;
}

int main(){
    int n;
    cin >> n;
    int total = 0;
    while(n--){
        string r;
        cin >> r;
        total += dec(r);
    }

    if(total >= 4000) cout << "ERROR" << endl;
    else{
        string ans = "";
        if(total >= 1000){
            for(int i = 0; i < total/1000; i++) ans += "M";
        }
        total %= 1000;
        if(total >= 900) ans += "CM";
        else if(total >= 500){
            ans += "D";
            total -= 500;
            for(int i = 0; i < total/100; i++) ans += "C";
        }else if(total >= 400) ans += "CD";
        else{
            for(int i = 0; i < total/100; i++) ans += "C";
        }

        total %= 100;
        if(total >= 90) ans += "XC";
        else if(total >= 50){
            ans += "L";
            total -= 50;
            for(int i = 0; i < total/10; i++) ans += "X";
        }else if(total >= 40) ans += "XL";
        else{
            for(int i = 0; i < total/10; i++) ans += "X";
        }

        total %= 10;
        if(total >= 9) ans += "IX";
        else if(total >= 5){
            ans += "V";
            total -= 5;
            for(int i = 0; i < total; i++) ans += "I";
        }else if(total >= 4) ans += "IV";
        else{
            for(int i = 0; i < total; i++) ans += "I";
        }

        cout << ans << endl;
    }
    return 0;
}
0