結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
158b
|
| 提出日時 | 2017-05-28 22:36:08 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,654 bytes |
| コンパイル時間 | 838 ms |
| コンパイル使用メモリ | 75,728 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 15:39:54 |
| 合計ジャッジ時間 | 1,557 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <climits>
#include <vector>
#include <numeric>
#include <complex>
using namespace std;
//#define __int64 long long
#define long __int64
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
const int Vecy[4] = {0,-1,0,1};
const int Vecx[4] = {1,0,-1,0};
const int eMax = 7;
char e[eMax] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
int n[eMax] = {1, 5, 10, 50, 100, 500, 1000};
int S1toN(char c){
for(int i=0; i<eMax; i++){
if(c == e[i]){
return n[i];
}
}
return -1;
}
int StoN(string str){
int res = 0;
str += 'I';
for(int i=0; i<str.length(); i++){
int n1 = S1toN(str[i]);
int n2 = S1toN(str[i+1]);
if(n1 >= n2){
res += n1;
}else{
res += (n2 - n1);
i ++;
}
}
return res - 1;
}
string N1toS(int n){
if(n == 0) return "";
int keta = log10(n);
int ki = n / pow(10, keta);
string res;
int base = keta * 2;
if(ki == 4){
res += e[base];
res += e[base + 1];
}else if(ki == 9){
res += e[base];
res += e[base + 2];
}else{
if(ki >= 5){
res += e[base + 1];
}
for(int i=0; i<ki%5; i++){
res += e[base];
}
}
return res;
}
string NtoS(int n){
string res;
if(n > 3999){
return "ERROR";
}
for(int i=3; i>=0; i--){
int n2 = n / pow(10, i);
//cout << "n2:" << n2 << endl;
res += N1toS(n2 * pow(10, i));
//cout << "res:" << res << endl;
n -= n2 * pow(10, i);
//cout << "n:" << n << endl;
}
return res;
}
int main(){
int n;
int ans = 0;
string sin;
cin >> n;
rep(i,n){
cin >> sin;
ans += StoN(sin);
}
cout << NtoS(ans) << endl;
return 0;
}
158b