結果

問題 No.518 ローマ数字の和
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-06-04 17:24:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,756 bytes
コンパイル時間 2,942 ms
コンパイル使用メモリ 147,792 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 19:52:10
合計ジャッジ時間 4,465 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.06.04 17:24:53
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int to_num(string s) {
    int res = 0;
    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == 'I' && s[i] == 'V') {
            s.erase(i-1,2);
            res += 4;
        }
    }
    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == 'I' && s[i] == 'X') {
            s.erase(i-1,2);
            res += 9;
        }
    }
    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == 'X' && s[i] == 'L') {
            s.erase(i-1,2);
            res += 40;
        }
    }
    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == 'X' && s[i] == 'C') {
            s.erase(i-1,2);
            res += 90;
        }
    }
    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == 'C' && s[i] == 'D') {
            s.erase(i-1,2);
            res += 400;
        }
    }
    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == 'C' && s[i] == 'M') {
            s.erase(i-1,2);
            res += 900;
        }
    }
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'I') {
            res += 1;
        }
        else if (s[i] == 'V') {
            res += 5;
        }
        else if (s[i] == 'X') {
            res += 10;
        }
        else if (s[i] == 'L') {
            res += 50;
        }
        else if (s[i] == 'C') {
            res += 100;
        }
        else if (s[i] == 'D') {
            res += 500;
        }
        else if (s[i] == 'M') {
            res += 1000;
        }
    }
    return res;
}
string to_arabia(int n) {
    if (n >= 4000) {
        return "ERROR";
    }
    string res;
    while (n >= 1000) {
        res += "M";
        n -= 1000;
    }
    if (n >= 900) {
        res += "CM";
        n -= 900;
    }
    if (n >= 500) {
        res += "D";
        n -= 500;
    }
    if (n >= 400) {
        res += "CD";
        n -= 400;
    }
    while(n >= 100) {
        res += "C";
        n -= 100;
    }
    if (n >= 90) {
        res += "XC";
        n -= 90;
    }
    if (n >= 50) {
        res += "L";
        n -= 50;
    }
    if (n >= 40) {
        res += "XL";
        n -= 40;
    }
    while(n >= 10) {
        res += "X";
        n -= 10;
    }
    if (n >= 9) {
        res += "IX";
        n -= 9;
    }
    if (n >= 5) {
        res += "V";
        n -= 5;
    }
    if (n >= 4) {
        res += "IV";
        n -= 4;
    }
    while(n >= 1) {
        res += "I";
        n -= 1;
    }
    return res;
}
int main() {
    int n;cin >> n;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        string s;cin >> s;
        sum += to_num(s);
    }
    cout << to_arabia(sum) << endl;
    return 0;
}
0