結果

問題 No.518 ローマ数字の和
ユーザー mogurockermogurocker
提出日時 2017-10-16 01:09:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,913 bytes
コンパイル時間 3,600 ms
コンパイル使用メモリ 148,052 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 05:26:56
合計ジャッジ時間 2,303 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i = 0; i < (n); i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
typedef long long ll;
typedef pair<int, int> P;

int n;
string t[6] = {"IV", "IX", "XL", "XC", "CD", "CM"};
char u[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
int tt[6] = {4, 9, 40, 90, 400, 900}, uu[7] = {1, 5, 10, 50, 100, 500, 1000};

int num(string s) {
	int pos = 0, ret = 0;
	while (pos < s.size()) {
		bool flag = false;
		if (pos < s.size()-1) {
			string sub = s.substr(pos, 2);
			FOR(i, 6) {
				if (sub == t[i]) {
					ret += tt[i];
					flag = true;
					pos += 2;
				}
			}
		}
		if (!flag) {
			FOR(i, 7) {
				if (s[pos] == u[i]) {
					ret += uu[i];
					pos++;
				}
			}
		}
	}
	return ret;
}

string str(int x) {
	string ret = "";
	if (x >= uu[6]) {
		ret += string(x/uu[6], u[6]);
		x %= uu[6];
	}
	if (x >= tt[5]) {
		ret += t[5];
		x -= tt[5];
	}
	if (x >= uu[5]) {
		ret += string(x/uu[5], u[5]);
		x -= uu[5];
	}
	if (x >= tt[4]) {
		ret += t[4];
		x -= tt[4];
	}
	if (x >= uu[4]) {
		ret += string(x/uu[4], u[4]);
		x %= uu[4];
	}
	if (x >= tt[3]) {
		ret += t[3];
		x -= tt[3];
	}
	if (x >= uu[3]) {
		ret += string(x/uu[3], u[3]);
		x %= uu[3];
	}
	if (x >= tt[2]) {
		ret += t[2];
		x -= tt[2];
	}
	if (x >= uu[2]) {
		ret += string(x/uu[2], u[2]);
		x %= uu[2];
	}
	if (x >= tt[1]) {
		ret += t[1];
		x -= tt[1];
	}
	if (x >= uu[1]) {
		ret += string(x/uu[1], u[1]);
		x %= uu[1];
	}
	if (x >= tt[0]) {
		ret += t[0];
		x -= tt[0];
	}
	if (x >= uu[0]) {
		ret += string(x/uu[0], u[0]);
		x %= uu[0];
	}
	return ret;
}

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin >> n;
	int ret = 0;
	FOR(i, n) {
		string s;
		cin >> s;
		ret += num(s);
	}
	if (ret >= 4000) cout << "ERROR" << endl;
	else cout << str(ret) << endl;
	return 0;
}
0