結果

問題 No.518 ローマ数字の和
ユーザー hotpepsihotpepsi
提出日時 2017-06-17 22:45:13
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 945 bytes
コンパイル時間 452 ms
コンパイル使用メモリ 61,336 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-08 20:48:50
合計ジャッジ時間 1,128 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int main(int argc, char *argv[]) {
	int n, tot = 0, vtbl[256] = {};
	vtbl['I'] = 1, vtbl['V'] = 5;
	vtbl['X'] = 10, vtbl['L'] = 50;
	vtbl['C'] = 100, vtbl['D'] = 500;
	vtbl['M'] = 1000;
	cin >> n;
	string s, digits = "IVXLCDM";
	for (int i = 0; i < n; ++i) {
		cin >> s;
		int prev = 5000;
		for (char c : s) {
			if (vtbl[c] > prev) {
				tot -= prev * 2;
			}
			tot += vtbl[c];
			prev = vtbl[c];
		}
	}
	string ans;
	if (tot >= 4000) {
		ans = "error";
	} else {
		for (int i = 6; i >= 0; --i) {
			while (tot >= vtbl[digits[i]]) {
				ans += digits[i];
				tot -= vtbl[digits[i]];
			}
			int j = 2 - (i % 2);
			if (i >= j && tot >= vtbl[digits[i]] - vtbl[digits[i - j]]) {
				ans += digits[i - j];
				ans += digits[i];
				tot -= vtbl[digits[i]] - vtbl[digits[i - j]];
			}
		}
	}
	cout << ans << endl;
	return 0;
}
0