結果

問題 No.518 ローマ数字の和
ユーザー どららどらら
提出日時 2017-05-28 22:03:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,059 bytes
コンパイル時間 1,674 ms
コンパイル使用メモリ 169,320 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:12:14
合計ジャッジ時間 2,515 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 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 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

0