結果

問題 No.518 ローマ数字の和
ユーザー Konton7Konton7
提出日時 2020-03-03 15:28:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,638 bytes
コンパイル時間 2,276 ms
コンパイル使用メモリ 198,760 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 00:06:55
合計ジャッジ時間 2,782 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define allpt(v) (v).begin(), (v).end()


const int mod = 1e9 + 7;
const string wsp = " ";
const string tb = "\t";
const string rt = "\n";

template <typename T>
void show1dvec(vector<T> v)
{
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << wsp;
    cout << v[n] << rt;
    return;
}

int rometoint(string s)
{
    int iflag, xflag, cflag;
    iflag = xflag = cflag = false;
    int n = s.length();
    int r = 0;
    rep(i, n)
    {
        if (s[n - 1 - i] == 'V' || s[n - 1 - i] == 'X')
            iflag = true;
        if (s[n - 1 - i] == 'L' || s[n - 1 - i] == 'C')
            xflag = true;
        if (s[n - 1 - i] == 'D' || s[n - 1 - i] == 'M')
            cflag = true;

        if (s[n - 1 - i] == 'I' && iflag)
            r -= 1;
        if (s[n - 1 - i] == 'I' && !iflag)
            r += 1;
        if (s[n - 1 - i] == 'V')
            r += 5;
        if (s[n - 1 - i] == 'X' && xflag)
            r -= 10;
        if (s[n - 1 - i] == 'X' && !xflag)
            r += 10;
        if (s[n - 1 - i] == 'L')
            r += 50;
        if (s[n - 1 - i] == 'C' && cflag)
            r -= 100;
        if (s[n - 1 - i] == 'C' && !cflag)
            r += 100;
        if (s[n - 1 - i] == 'D')
            r += 500;
        if (s[n - 1 - i] == 'M')
            r += 1000;
    }
    return r;
}

string inttorome(int x)
{
    int y;
    string ret;
    vector<string> charlist = {"M", " ", "C", "D", "X", "L", "I", "V"};
    rep(i, 4)
    {
        y = (x / 1000) % 10;
        x *= 10;
        if (y % 5 == 4)
        {
            ret += charlist[2 * i];
            if (y == 9)
                ret += charlist[2 * i - 2];
            else
                ret += charlist[2 * i + 1];
        }
        else
        {
            if (y >= 5)
            {
                ret += charlist[2 * i + 1];
                y -= 5;
            }
            rep(j, y) ret += charlist[2 * i];
        }
    }
    return ret;
}

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, total;
    string s;
    total = 0;
    cin >> n;
    rep(i, n)
    {
        cin >> s;
        total += rometoint(s);
    }

    if (total >= 4000)
        cout << "ERROR" << rt;
    else
        cout << inttorome(total) << rt;

    return 0;
}
0