結果

問題 No.518 ローマ数字の和
ユーザー nukachanukacha
提出日時 2017-10-18 02:18:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,644 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 67,072 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 07:07:05
合計ジャッジ時間 1,498 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>

int decode_char(char c) {
    int n = 0;
    switch (c) {
        case 'I':
            n += 1;
            break;
        case 'V':
            n += 5;
            break;
        case 'X':
            n += 10;
            break;
        case 'L':
            n += 50;
            break;
        case 'C':
            n += 100;
            break;
        case 'D':
            n += 500;
            break;
        case 'M':
            n += 1000;
            break;
        default:
            break;
    }
    return n;
}

int decode(std::string r) {
    using namespace std;
    int n = 0;
    char tmp = '\0';
    for (char c: r) {
        if (tmp == 'V' || tmp == 'L' || tmp == 'D' || tmp =='M') {
            n += decode_char(tmp);
            tmp = '\0';
        }
        if (tmp == '\0') {
            if (c == 'V' || c == 'L' || c == 'D' || c =='M') {
                n += decode_char(c);
                continue;
            } else if (c == 'I' || c == 'X' || c == 'C') {
                tmp = c;
                continue;
            }
        } else if (tmp == 'I') {
            tmp = '\0';
            switch (c) {
                case 'V':
                    n += 4;
                    break;
                case 'X':
                    n += 9;
                    break;
                default:
                    n += 1;
                    tmp = c;
                    break;
            }
        } else if (tmp == 'X') {
            tmp = '\0';
            switch (c) {
                case 'L':
                    n += 40;
                    break;
                case 'C':
                    n += 90;
                    break;
                default:
                    n += 10;
                    tmp = c;
                    break;
            } 
        } else if (tmp == 'C') {
            tmp = '\0';
            switch (c) {
                case 'D':
                    n += 400;
                    break;
                case 'M':
                    n += 900;
                    break;
                default:
                    n += 100;
                    tmp = c;
                    break;
            }
        }
    }
    n += decode_char(tmp);
    tmp = '\0';
    return n;
}

std::string encode(int n) {
    using namespace std;
    string r;
    while (n > 999) {
        r += "M";
        n -= 1000;
    }
    if (n / 100 == 9) {
        r += "CM";
        n -= 900;
    } else if (n / 100 == 4) {
        r += "CD";
        n -= 400;
    } else {
        if (n / 100 > 4) {
            r += "D";
            n -= 500;
        }
        while (n > 99) {
            r += "C";
            n -= 100;
        }
    }
    if (n / 10 == 9) {
        r += "XC";
        n -= 90;
    } else if (n / 10 == 4) {
        r += "XL";
        n -= 40;
    } else {
        if (n / 10 > 4) {
            r += "L";
            n -= 50;
        }
        while (n > 9) {
            r += "X";
            n -= 10;
        }
    }
    if (n == 9) {
        r += "IX";
        n -= 9;
    } else if (n == 4) {
        r += "IV";
        n -= 4;
    } else {
        if (n > 4) {
            r += "V";
            n -= 5;
        }
        while (n > 0) {
            r += "I";
            n -= 10;
        }
    }
    return r;
}

int main() {
    using namespace std;
    int n = 0;
    int sum = 0;
    cin >> n;
    string r[n];
    for (string &ri: r) {
        cin >> ri;
    } 
    for (const string ri: r) {
        sum += decode(ri);
    }
    if (sum > 3999) {
        cout << "ERROR" << endl;
        return 0;
    }
    cout << encode(sum) << endl;
}
0