結果

問題 No.518 ローマ数字の和
ユーザー gigimegigime
提出日時 2017-05-28 22:01:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 1,896 ms
コンパイル使用メモリ 176,708 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:12:07
合計ジャッジ時間 2,811 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 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 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 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;

#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;

int N;
map<char,int> mp{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
int A [] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
string B [] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

string toRoma(int val)
{
	string res;
	FOR(i,0,13){
		while(val >= A [i]){
			res += B [i];
			val -= A [i];
		}
	}
	return res;
}

int fromRoma(string str)
{
	int n = str.size();
	int res = 0;
	FOR(i,0,n){
		if(i == n - 1 || mp [str [i]] >= mp [str [i + 1]]){
			res += mp [str [i]];
		}
		else{
			res -= mp [str [i]];
		}
	}
	return res;
}

int main()
{
	scanf("%d",&N);
	int sum = 0;
	FOR(i,0,N){
		string S;
		cin >> S;
		sum += fromRoma(S);
	}

	if(sum >= 4000){
		puts("ERROR");
	}
	else{
		printf("%s\n",toRoma(sum).c_str());
	}

	return 0;
}
0