結果

問題 No.518 ローマ数字の和
ユーザー gigime
提出日時 2017-05-28 22:01:10
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 177,484 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-21 15:26:20
合計ジャッジ時間 2,671 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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