結果

問題 No.518 ローマ数字の和
ユーザー TautConyTautCony
提出日時 2017-11-28 00:34:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,484 bytes
コンパイル時間 2,827 ms
コンパイル使用メモリ 73,940 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 08:13:31
合計ジャッジ時間 2,471 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

vector<int> range {1000,500,100,50,10,5,1};

int toNum(char rouma)
{
    switch(rouma)
    {
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
    }
    throw;
}

inline int toNum(string rouma)
{
    int ret = 0;
    int last = 0;
    for(char c : rouma)
    {
        int curr = toNum(c);
        ret += curr;
        if (curr > last)
        {
            ret -= last*2;
        }
        last = curr;
    }
    return ret;
}

inline char getRouma(int num)
{
    switch(num)
    {
        case 1   : return 'I';
        case 5   : return 'V';
        case 10  : return 'X';
        case 50  : return 'L';
        case 100 : return 'C';
        case 500 : return 'D';
        case 1000: return 'M';
    }
    throw;
}

inline char getNext(int num)
{
    switch(num)
    {
        case 1   : return 'V';
        case 5   : return 'X';
        case 10  : return 'L';
        case 50  : return 'C';
        case 100 : return 'D';
        case 500 : return 'M';
    }
    throw;
}

string toRouma(int num)
{
    if (num > 3999) return "ERROR";
    vector<int> niseMap(1024);
    for(int i : range)
    {
        while(num >= i)
        {
            ++niseMap[i];
            num -= i;
        }
    }
    string ret;
    for(int i : range)
    {
        int cnt = niseMap[i];
        if (cnt != 0)
        {
            if (cnt == 4)
            {
                if (*ret.rbegin() == getNext(i))
                {
                    //ret = "V"; i = 1; cnt = 4;
                    //VIV => IX
                    ret.pop_back();
                    ret += getRouma(i);
                    ret += getNext(toNum(getNext(i)));
                }
                else
                {
                    //IIII => IV;
                    ret += getRouma(i);
                    ret += getNext(i);
                }
            }
            else
            {
                ret += string(cnt, getRouma(i));
            }
        }
    }
    return ret;
}

int main()
{
    int n;
    cin >> n;
    vector<string> arr(n);
    int sum = 0;
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        int num = toNum(arr[i]);
        sum += num;
        //cout << num << endl;
    }
    //cout << sum << endl;
    cout << toRouma(sum) << endl;
}
0