結果

問題 No.518 ローマ数字の和
ユーザー Hoget157Hoget157
提出日時 2017-05-28 21:56:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,245 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 81,188 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:10:14
合計ジャッジ時間 1,567 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 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 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 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 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 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;
	int num[] = {1000,500,100,50,10,5,1};
	char c[] = {'M','D','C','L','X','V','I'};
	string chg[] = {"DCCCC","CCCC","LXXXX","XXXX","VIIII","IIII"};
	string to[] = {"CM   ","CD  ","XC   ","XL  ","IX   ","IV  "};
	for(int i = 0;i < 7;i++){
		for(int j = 0;j < n / num[i];j++) s += c[i];
		n %= num[i];
	}
	for(int i = 0;i < 6;i++){
		for(int j = 0;j < (int)s.length() - (int)chg[i].length() + 1;j++){
			if(s.substr(j,chg[i].length()) == chg[i]){
				for(int k = 0;k < chg[i].length();k++) s[j + k] = to[i][k];
			}
		}
	}
	string t;
	for(int i = 0;i < s.length();i++){
		if(s[i] != ' ') t += s[i];
	}
	return t;
}

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