結果

問題 No.518 ローマ数字の和
ユーザー snrnsidy
提出日時 2021-07-15 21:59:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 2,516 bytes
コンパイル時間 2,070 ms
コンパイル使用メモリ 207,132 KB
最終ジャッジ日時 2025-01-23 01:10:27
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
map <string, int> m;
int convert(string s)
{
int n = s.length();
int val = 0;
int idx = 0;
while (idx < n)
{
if (s[idx] == 'I')
{
if (s.substr(idx, 2) == "IV")
{
val += m[s.substr(idx, 2)];
idx += 2;
}
else if (s.substr(idx, 2) == "IX")
{
val += m[s.substr(idx, 2)];
idx += 2;
}
else
{
val += m[s.substr(idx, 1)];
idx++;
}
}
else if (s[idx] == 'X')
{
if (s.substr(idx, 2) == "XL")
{
val += m[s.substr(idx, 2)];
idx += 2;
}
else if (s.substr(idx, 2) == "XC")
{
val += m[s.substr(idx, 2)];
idx += 2;
}
else
{
val += m[s.substr(idx, 1)];
idx++;
}
}
else if (s[idx] == 'C')
{
if (s.substr(idx, 2) == "CD")
{
val += m[s.substr(idx, 2)];
idx += 2;
}
else if (s.substr(idx, 2) == "CM")
{
val += m[s.substr(idx, 2)];
idx += 2;
}
else
{
val += m[s.substr(idx, 1)];
idx++;
}
}
else
{
val += m[s.substr(idx, 1)];
idx++;
}
}
return val;
}
string convert2(int num)
{
string res;
while (num > 0)
{
if (num >= 1000)
{
num -= 1000;
res += "M";
}
else if (num >= 900 && num < 1000)
{
num -= 900;
res += "CM";
}
else if (num >= 500 && num < 900)
{
num -= 500;
res += "D";
}
else if (num >= 400 && num < 500)
{
num -= 400;
res += "CD";
}
else if (num >= 100 && num < 400)
{
num -= 100;
res += "C";
}
else if (num >= 90 && num < 100)
{
num -= 90;
res += "XC";
}
else if (num >= 50 && num < 90)
{
num -= 50;
res += "L";
}
else if (num >= 40 && num < 50)
{
num -= 40;
res += "XL";
}
else if (num >= 10 && num < 40)
{
num -= 10;
res += "X";
}
else if (num >= 9 && num < 10)
{
num -= 9;
res += "IX";
}
else if (num >= 5 && num < 9)
{
num -= 5;
res += "V";
}
else if (num >= 4 && num < 5)
{
num -= 4;
res += "IV";
}
else
{
num -= 1;
res += "I";
}
}
return res;
}
int main(void)
{
m["I"] = 1;
m["V"] = 5;
m["X"] = 10;
m["L"] = 50;
m["C"] = 100;
m["D"] = 500;
m["M"] = 1000;
m["IV"] = 4;
m["IX"] = 9;
m["XL"] = 40;
m["XC"] = 90;
m["CD"] = 400;
m["CM"] = 900;
cin.tie(0);
ios::sync_with_stdio(false);
int res = 0;
int n;
string a;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a;
res += convert(a);
}
if (res >= 4000)
{
cout << "ERROR" << '\n';
}
else
{
cout << convert2(res) << '\n';
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0