結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-05-28 21:44:42 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,067 bytes |
| コンパイル時間 | 761 ms |
| コンパイル使用メモリ | 80,024 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-21 15:21:28 |
| 合計ジャッジ時間 | 1,550 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 WA * 6 |
ソースコード
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<char,int> mp;
int to_int(string s){
int prev = 0,sum = 0;
for(int i = s.length() - 1;i >= 0;i--){
int add = mp[s[i]];
if(add < prev) sum -= add;
else sum += add;
prev = add;
}
return sum;
}
string to_rome(int n){
string s;
char c[] = {'C','X','I'},cc[] = {'M','D','C','L','X','V'};
int num = 1000;
for(int i = 0;i < 3;i++){
if(n % num == num - num / 10){
s += c[i];
n += num / 10;
}
for(int j = 0;j < n / num;j++) s += cc[i * 2];
n %= num;
if(n % (num / 2) == num / 2 - num / 10){
s += c[i];
n += num / 10;
}
if(n >= num / 2) s += cc[i * 2 + 1];
n %= num / 2;
num /= 10;
}
for(int i = 0;i < n;i++) s += 'I';
return s;
}
int main(){
int n;
cin >> n;
mp['I'] = 1;
mp['V'] = 5;
mp['X'] = 10;
mp['L'] = 50;
mp['C'] = 100;
mp['D'] = 500;
mp['M'] = 1000;
int ans = 0;
for(int i = 0;i < n;i++){
string s;
cin >> s;
ans += to_int(s);
}
if(ans >= 4000) cout << "ERROR" << endl;
else cout << to_rome(ans) << endl;
return 0;
}