結果

問題 No.518 ローマ数字の和
ユーザー oshiborioshibori
提出日時 2017-05-28 22:28:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,908 bytes
コンパイル時間 1,336 ms
コンパイル使用メモリ 164,144 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:22:46
合計ジャッジ時間 2,149 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
int cov(char c) {
	if (c == 'I')return 1;
	else if (c == 'V')return 5;
	else if (c == 'X')return 10;
	else if (c == 'L')return 50;
	else if (c == 'C')return 100;
	else if (c == 'D')return 500;
	else return 1000;
}
int arab(string s) {
	int ret(0);
	rep(i, 0, s.size() - 1) {
		if (cov(s[i]) < cov(s[i + 1]))ret -= cov(s[i]);
		else ret += cov(s[i]);
	}
	ret += cov(s.back());
	return ret;
}
string rome(int a) {
	if (a > 3999)return "ERROR";
	string ret;
	int x = a / 1000;
	while (x--)ret += "M";
	a %= 1000;
	if (a / 100 * 100 == 900)ret += "CM", a -= 900;
	else if (a / 100 * 100 == 400)ret += "CD", a -= 400;
	else {
		if (a >= 500)ret += "D", a -= 500;
		int y = a / 100;
		while (y--)ret += "C", a -= 100;
	}
	if (a / 10 * 10 == 90)ret += "XC", a -= 90;
	else if (a / 10 * 10 == 40)ret += "XL", a -= 40;
	else {
		if (a >= 50)ret += "L", a -= 50;
		int c = a / 10;
		while (c--)ret += "X", a -= 10;
	}
	if (a == 9)ret += "IX", a -= 9;
	else if (a == 4)ret += "IV", a -= 4;
	else {
		if (a >= 5)ret += "V", a -= 5;
		while (a--)ret += "I";
	}
	return ret;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N; cin >> N;
	vector<string>v(N); rep(i, 0, N)cin >> v[i];
	int sum(0);
	rep(i, 0, v.size())sum += arab(v[i]);
	cout << rome(sum) << endl;
	return 0;
}
0