結果

問題 No.518 ローマ数字の和
ユーザー mbanmban
提出日時 2017-05-29 11:14:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,558 bytes
コンパイル時間 1,126 ms
コンパイル使用メモリ 113,540 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 16:48:27
合計ジャッジ時間 2,425 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 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 1 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 2 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;

map<char, int> mp = {{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50},
                     {'X', 10},   {'V', 5},   {'I', 1}};
string s[]{"M",  "CM", "D",  "CD", "C",  "XC", "L",
           "XL", "X",  "IX", "V",  "IV", "I"};
int num[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

int to_num(string s) {
    int result = 0;
    int size = s.size();
    for (int i = 0; i < size; i++) {
        if (i < size - 1 && mp[s[i]] < mp[s[i + 1]]) {
            result -= mp[s[i]];
        } else {
            result += mp[s[i]];
        }
    }
    return result;
}

string to_roma(int i) {
    string result = "";
    int index = 0;
    while (i > 0) {
        if (num[index] <= i) {
            i -= num[index];
            result += s[index];
        } else {
            index++;
        }
    }
    return result;
}

int main() {
    int N;
    cin >> N;
    vector<string> R(N);
    for (size_t i = 0; i < N; i++) {
        cin >> R[i];
    }
    int sum = 0;
    for (size_t i = 0; i < N; i++) {
        sum += to_num(R[i]);
    }
    if (sum > 3999) {
        cout << "ERROR" << endl;
        return 0;
    } else {
        cout << to_roma(sum) << endl;
        return 0;
    }
}
0