結果

問題 No.518 ローマ数字の和
ユーザー Hoget157Hoget157
提出日時 2017-05-28 21:44:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,067 bytes
コンパイル時間 728 ms
コンパイル使用メモリ 79,244 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:06:57
合計ジャッジ時間 1,597 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
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 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 WA -
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 WA -
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 <iostream>
#include <string>
#include <map>
using namespace std;

map<char,int> mp;

int to_int(string s){
	int prev = 0,sum = 0;
	for(int i = s.length() - 1;i >= 0;i--){
		int add = mp[s[i]];
		if(add < prev) sum -= add;
		else sum += add;
		prev = add;
	}
	return sum;
}

string to_rome(int n){
	string s;
	char c[] = {'C','X','I'},cc[] = {'M','D','C','L','X','V'};
	int num = 1000;
	for(int i = 0;i < 3;i++){
		if(n % num == num - num / 10){
			s += c[i];
			n += num / 10;
		}
		for(int j = 0;j < n / num;j++) s += cc[i * 2];
		n %= num;
		if(n % (num / 2) == num / 2 - num / 10){
			s += c[i];
			n += num / 10;
		}
		if(n >= num / 2) s += cc[i * 2 + 1];
		n %= num / 2;
		num /= 10;
	}
	for(int i = 0;i < n;i++) s += 'I';
	return s;
}

int main(){
	int n;
	cin >> n;
	mp['I'] = 1;
	mp['V'] = 5;
	mp['X'] = 10;
	mp['L'] = 50;
	mp['C'] = 100;
	mp['D'] = 500;
	mp['M'] = 1000;
	int ans = 0;
	for(int i = 0;i < n;i++){
		string s;
		cin >> s;
		ans += to_int(s);
	}
	if(ans >= 4000) cout << "ERROR" << endl;
	else cout << to_rome(ans) << endl;
	return 0;
}
0