結果

問題 No.518 ローマ数字の和
ユーザー kotatsugamekotatsugame
提出日時 2019-10-12 14:54:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,054 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 66,384 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 16:26:25
合計ジャッジ時間 1,880 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:37:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   37 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
using namespace std;
int f(const string&s)
{
	int ret=0;
	int pre=-114514;
	for(int i=0;i<s.size();i++)
	{
		switch(s[i]){
			case'I':ret+=1;pre=1;break;
			case'V':ret+=pre==1?3:5;pre=5;break;
			case'X':ret+=pre==1?8:10;pre=10;break;
			case'L':ret+=pre==10?30:50;pre=50;break;
			case'C':ret+=pre==10?80:100;pre=100;break;
			case'D':ret+=pre==100?300:500;pre=500;break;
			case'M':ret+=pre==100?800:1000;pre=1000;break;
		}
	}
	return ret;
}
string g(char a,char b,char c,int t)
{
	string ret="";
	if(t<4)
	{
		for(int i=0;i<t;i++)ret+=a;
	}
	else if(t==4)ret+=a,ret+=b;
	else if(t<9)
	{
		ret+=b;
		for(int i=5;i<t;i++)ret+=a;
	}
	else ret+=a,ret+=c;
	return ret;
}
main()
{
	int N;cin>>N;
	int ans=0;
	for(;N--;)
	{
		string s;cin>>s;ans+=f(s);
	}
	if(ans>=4000)
	{
		cout<<"ERROR"<<endl;
		return 0;
	}
	string ret="";
	if(ans/1000)ret+=g('M','0','0',ans/1000);
	ans%=1000;
	if(ans/100)ret+=g('C','D','M',ans/100);
	ans%=100;
	if(ans/10)ret+=g('X','L','C',ans/10);
	ans%=10;
	if(ans)ret+=g('I','V','X',ans);
	cout<<ret<<endl;
}
0