結果

問題 No.518 ローマ数字の和
ユーザー tubo28tubo28
提出日時 2017-05-28 21:32:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,751 bytes
コンパイル時間 3,448 ms
コンパイル使用メモリ 184,404 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:03:17
合計ジャッジ時間 2,663 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 1 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 1 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>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)
const int MOD = 1000000007;

template <typename T>
void __dump__(std::ostream &os, const T &first) {
    os << first;
}
template <typename First, typename... Rest>
void __dump__(std::ostream &os, const First &first, const Rest &... rest) {
    os << first << ", ";
    __dump__(os, rest...);
}
#define dump(...)                                         \
    do {                                                  \
        std::ostringstream os;                            \
        os << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
        __dump__(os, __VA_ARGS__);                        \
        std::cerr << os.str() << std::endl;               \
    } while (0)

int from_roman(const std::string &s) {
    static const std::map<char, int> m = {{'I', 1},   {'V', 5},   {'X', 10},  {'L', 50},
                                          {'C', 100}, {'D', 500}, {'M', 1000}};
    static const auto get = [&](char c) {
        assert(m.find(c) != m.end());
        return m.find(c)->second;
    };
    int n = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
        if (i + 1 == (int)s.size() || get(s[i]) >= get(s[i + 1]))
            n += get(s[i]);
        else
            n -= get(s[i]);
    }
    return n;
}

std::string to_roman(int n) {
    static const std::map<int, std::string> m = {{1, "I"},   {5, "V"},   {10, "X"},  {50, "L"},
                                                 {100, "C"}, {500, "D"}, {1000, "M"}};
    static const auto get = [&](int c) {
        assert(m.find(c) != m.end());
        return m.find(c)->second;
    };
    std::string res;
    int p = 1;
    while (n) {
        int d = n % 10;
        int f = d % 5;
        std::string s = "";
        if (f == 0) {
        } else if (f <= 3) {
            for (int i = 0; i < f; ++i) s += get(p);
        } else if (f == 4) {
            s += get(p);
        }
        if (f <= 3) {
            if (d >= 5) s = get(p * 5) + s;
        } else {
            if (d >= 5)
                s += get(p * 10);
            else
                s += get(p * 5);
        }
        res = s + res;
        p *= 10;
        n /= 10;
    }
    return res;
}

int main() {
    int n;
    while (cin >> n) {
        int sum = 0;
        rep(i, n) {
            string s;
            cin >> s;
            sum += from_roman(s);
        }
        cout << (sum >= 4000 ? "ERROR" : to_roman(sum).c_str()) << endl;
    }
}
0