結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
どらら
|
| 提出日時 | 2017-05-28 22:03:56 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,059 bytes |
| コンパイル時間 | 1,509 ms |
| コンパイル使用メモリ | 168,788 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 15:26:28 |
| 合計ジャッジ時間 | 2,294 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;
int roma2arab(const string& str){
int ret = 0;
int i = 0;
while(i<str.size()){
bool flg = false;
if(i+1<str.size()){
if(str.substr(i,2) == "IV"){ ret += 4; flg = true; }
else if(str.substr(i,2) == "IX"){ ret += 9; flg = true; }
else if(str.substr(i,2) == "XL"){ ret += 40; flg = true; }
else if(str.substr(i,2) == "XC"){ ret += 90; flg = true; }
else if(str.substr(i,2) == "CD"){ ret += 400; flg = true; }
else if(str.substr(i,2) == "CM"){ ret += 900; flg = true; }
}
if(flg){
i += 2;
}else{
if(str[i] == 'I') ret += 1;
else if(str[i] == 'V') ret += 5;
else if(str[i] == 'X') ret += 10;
else if(str[i] == 'L') ret += 50;
else if(str[i] == 'C') ret += 100;
else if(str[i] == 'D') ret += 500;
else if(str[i] == 'M') ret += 1000;
i++;
}
}
return ret;
}
string arab2roma(int x){
string ret = "";
while(x>0){
if(x>=1000){ ret += "M"; x -= 1000; }
else if(x>=900){ ret += "CM"; x -= 900; }
else if(x>=500){ ret += "D"; x -= 500; }
else if(x>=400){ ret += "CD"; x -= 400; }
else if(x>=100){ ret += "C"; x -= 100; }
else if(x>=90){ ret += "XC"; x -= 90; }
else if(x>=50){ ret += "L"; x -= 50; }
else if(x>=40){ ret += "XL"; x -= 40; }
else if(x>=10){ ret += "X"; x -= 10; }
else if(x>=9){ ret += "IX"; x -= 9; }
else if(x>=5){ ret += "V"; x -= 5; }
else if(x>=4){ ret += "IV"; x -= 4; }
else if(x>=1){ ret += "I"; x -= 1; }
}
return ret;
}
int main(){
int N;
cin >> N;
int sum = 0;
rep(i,N){
string str;
cin >> str;
sum += roma2arab(str);
}
if(sum >= 4000){
cout << "ERROR" << endl;
}else{
cout << arab2roma(sum) << endl;
}
return 0;
}
どらら