結果

問題 No.518 ローマ数字の和
ユーザー YukiDarumaYukiDaruma
提出日時 2017-05-28 22:57:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,823 bytes
コンパイル時間 1,591 ms
コンパイル使用メモリ 169,108 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:29:29
合計ジャッジ時間 2,454 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 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;

namespace
{
int ValRome( const char *lpRome )
{
int i;
int iVal;

	iVal = 0;
	i = 0;
	while( lpRome[ i ] == 'M' )
	{
		iVal += 1000;
		i++;
	}

	if( lpRome[ i ] == 'D' )
	{
		iVal += 500;
		i++;
	}

	if( lpRome[ i ] == 'C' )
	{
		i++;
		if( lpRome[ i ] == 'M' )
		{
			iVal += 900;
			i++;
		}
		else if( lpRome[ i ] == 'D' )
		{
			iVal += 400;
			i++;
		}
		else
			iVal += 100;

		while( lpRome[ i ] == 'C' )
		{
			iVal += 100;
			i++;
		}
	}

	if( lpRome[ i ] == 'L' )
	{
		iVal += 50;
		i++;
	}

	if( lpRome[ i ] == 'X' )
	{
		i++;
		if( lpRome[ i ] == 'C' )
		{
			iVal += 90;
			i++;
		}
		else if( lpRome[ i ] == 'L' )
		{
			iVal += 40;
			i++;
		}
		else
			iVal += 10;

		while( lpRome[ i ] == 'X' )
		{
			iVal += 10;
			i++;
		}
	}

	if( lpRome[ i ] == 'V' )
	{
		iVal += 5;
		i++;
	}

	if( lpRome[ i ] == 'I' )
	{
		i++;
		if( lpRome[ i ] == 'X' )
		{
			iVal += 9;
			i++;
		}
		else if( lpRome[ i ] == 'V' )
		{
			iVal += 4;
			i++;
		}
		else
			iVal += 1;

		while( lpRome[ i ] == 'I' )
		{
			iVal += 1;
			i++;
		}
	}

	return iVal;
}

void StrRome( const int iRome )
{
int iVal;

	iVal = iRome;
	if( iVal > 3999 ) return;

	while( iVal >= 1000 )
	{
		cout << "M";
		iVal -= 1000;
	}

	if( iVal >= 900 )
	{
		cout << "CM";
		iVal -= 900;
	}
	else if( iVal >= 800 )
	{
		cout << "DCCC";
		iVal -= 800;
	}
	else if( iVal >= 700 )
	{
		cout << "DCC";
		iVal -= 700;
	}
	else if( iVal >= 600 )
	{
		cout << "DC";
		iVal -= 600;
	}
	else if( iVal >= 500 )
	{
		cout << "D";
		iVal -= 500;
	}
	else if( iVal >= 400 )
	{
		cout << "CD";
		iVal -= 400;
	}

	while( iVal >= 100 )
	{
		cout << "C";
		iVal -= 100;
	}


	if( iVal >= 90 )
	{
		cout << "XC";
		iVal -= 90;
	}
	else if( iVal >= 80 )
	{
		cout << "LXXX";
		iVal -= 80;
	}
	else if( iVal >= 70 )
	{
		cout << "LXX";
		iVal -= 70;
	}
	else if( iVal >= 60 )
	{
		cout << "LX";
		iVal -= 60;
	}
	else if( iVal >= 50 )
	{
		cout << "L";
		iVal -= 50;
	}
	else if( iVal >= 40 )
	{
		cout << "XL";
		iVal -= 40;
	}

	while( iVal >= 10 )
	{
		cout << "X";
		iVal -= 10;
	}

	if( iVal >= 9 )
	{
		cout << "IX";
		iVal -= 9;
	}
	else if( iVal >= 8 )
	{
		cout << "VIII";
		iVal -= 8;
	}
	else if( iVal >= 7 )
	{
		cout << "VII";
		iVal -= 7;
	}
	else if( iVal >= 6 )
	{
		cout << "VI";
		iVal -= 6;
	}
	else if( iVal >= 5 )
	{
		cout << "V";
		iVal -= 5;
	}
	else if( iVal >= 4 )
	{
		cout << "IV";
		iVal -= 4;
	}

	while( iVal >= 1 )
	{
		cout << "I";
		iVal -= 1;
	}

	cout << endl;
}

}		// namespace

int main( int argc, char *argv[] )
{
int i;
int N;
char R[ 110 ];
int iSum;


	ios::sync_with_stdio( false );
	cin.tie( 0 );

	cin >> N;

	iSum = 0;
	for( i = 0; i < N; i++ )
	{
		cin >> R;
		iSum += ValRome( R );
	}

	if( iSum > 3999 )
		cout << "ERROR" << endl;
	else
		StrRome( iSum );

	return 0;
}


0