結果

問題 No.518 ローマ数字の和
ユーザー rtomprtomp
提出日時 2017-06-02 17:28:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,418 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 161,308 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 20:48:05
合計ジャッジ時間 3,166 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

const int romaNum[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
const char *romaChar[13] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

inline string convertToRoma(int n){
    
    if(n > 3999) return "ERROR";
    
    int cnt;
    string ret;
    
    for(int i = 0; n ; ++i) 
    {
        // cout << romaChar[i] << " = " << romaNum[i] << endl;
        cnt = n / romaNum[i];
        
        for(int r = 0 ; r <cnt ; ++r) ret += romaChar[i];
        
        n = n - cnt * romaNum[i];
    };
    
    return ret;
}

inline int convertFromRoma(string r){
    
    int ret = 0, pre = 10000;
    
    for(int ri = 0 ; ri < r.size() ; ++ri)
    {
        for(int ci = 0; ci < 13 ; ci += 2) 
        {
            if(r[ri] == *romaChar[ci])
            {
                if(pre < romaNum[ci]) ret += romaNum[ci] - pre * 2;
                else ret += romaNum[ci];
                
                pre = romaNum[ci];
                
                break;
            }
        };
    }
    
    return ret;
}

void Solve()
{
    double X,Y,pre,later,secX,secY,OneMaterX;
    int N,ans = 0,cnt;
    string R;
    cin >> N;
    
    while(N--)
    {
        cin >> R;
        ans += convertFromRoma(R);
    }
    
    cout << convertToRoma(ans) << endl;
}

int main(void){
    
    ios::sync_with_stdio(0);
    cin.tie(0);
    Solve();
    return 0;
}
  
0