結果

問題 No.518 ローマ数字の和
ユーザー le_panda_noirle_panda_noir
提出日時 2020-06-14 16:12:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,272 bytes
コンパイル時間 906 ms
コンパイル使用メモリ 89,664 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 19:35:58
合計ジャッジ時間 2,299 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <stack>
#include <cmath>

using namespace std;
#define in(v) v; cin >> v;
#define rep(i,n) for(int i=0,_i=(n);i<_i;++i)
#define rrep(i,n) for(int i=(n);i>=0;--i)

int main() {
  int N; cin >> N;

  int ans = 0;
  char cur[5] = {'I', 'X', 'C', 'M', ' '}, mid[4] = {'V', 'L', 'D', ' '};
  rep(i, N) {
    string in(a);
    int idx = 0;
    rrep(j, 3) {
      int k = idx;
      while (k < a.size() && (a[k] == cur[j] || a[k] == mid[j] || a[k] == cur[j+1]))
        ++k;
      string S = a.substr(idx, k-idx); idx = k;
      int n;
      if (S == string(1, cur[j]) + cur[j+1])    n = 9;
      else if (S == string(1, cur[j]) + mid[j]) n = 4;
      else if (S.find(mid[j]) == string::npos)  n = S.length();
      else n = S.length() + 4;
      ans += n * pow(10, j);
    }
  }
  if (ans > 3999) {
    cout << "ERROR\n";
    return 0;
  }
  stack<string> ans_s;
  rep(i, 4) {
    int n = ans % 10; ans /= 10;
    string s;
    if (n == 9)
      (s += cur[i]) += cur[i+1];
    else if (n == 4)
      (s += cur[i]) += mid[i];
    else {
      if (n >= 5)
        n -= 5, s += mid[i];
      rep(k, n)
        s += cur[i];
    }
    ans_s.push(s);
  }
  while (!ans_s.empty()) {
    cout << ans_s.top(); ans_s.pop();
  }
  cout << endl;

  return 0;
}
0