結果

問題 No.518 ローマ数字の和
ユーザー 158b158b
提出日時 2017-05-28 22:36:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,654 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 76,040 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:25:04
合計ジャッジ時間 1,468 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <climits>
#include <vector>
#include <numeric>
#include <complex>
using namespace std;

//#define __int64 long long
#define long __int64
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
const int Vecy[4] = {0,-1,0,1};
const int Vecx[4] = {1,0,-1,0};

const int eMax = 7;
char e[eMax] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
int n[eMax] = {1, 5, 10, 50, 100, 500, 1000};

int S1toN(char c){
	for(int i=0; i<eMax; i++){
		if(c == e[i]){
			return n[i];
		}
	}
	
	return -1;
}

int StoN(string str){
	int res = 0;
	str += 'I';
	
	for(int i=0; i<str.length(); i++){
		int n1 = S1toN(str[i]);
		int n2 = S1toN(str[i+1]);
		if(n1 >= n2){
			res += n1;
		}else{
			res += (n2 - n1);
			i ++;
		}
	}
	
	return res - 1;
}

string N1toS(int n){
	if(n == 0) return "";
	int keta = log10(n);
	int ki = n / pow(10, keta);
	string res;
	int base = keta * 2;
	
	if(ki == 4){
		res += e[base];
		res += e[base + 1];
	}else if(ki == 9){
		res += e[base];
		res += e[base + 2];
	}else{
		if(ki >= 5){
			res += e[base + 1];
		}
		for(int i=0; i<ki%5; i++){
			res += e[base];
		}
	}
	
	return res;
}

string NtoS(int n){
	string res;
	
	if(n > 3999){
		return "ERROR";
	}
	
	for(int i=3; i>=0; i--){
		int n2 = n / pow(10, i);
		//cout << "n2:" << n2 << endl;
		res += N1toS(n2 * pow(10, i));
		//cout << "res:" << res << endl;
		n -= n2 * pow(10, i);
		//cout << "n:" << n << endl;
	}
	
	return res;
	
}

int main(){
	int n;
	int ans = 0;
	string sin;
	
	cin >> n;
	rep(i,n){
		cin >> sin;
		ans += StoN(sin);
	}
    
	cout << NtoS(ans) << endl;
	
    return 0;
}
0