結果

問題 No.518 ローマ数字の和
ユーザー yukudoyukudo
提出日時 2017-05-28 21:35:14
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,909 bytes
コンパイル時間 1,405 ms
コンパイル使用メモリ 163,348 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:04:35
合計ジャッジ時間 2,377 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 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 1 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 1 ms
4,348 KB
testcase_13 WA -
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 WA -
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int nextInt()’:
main.cpp:11:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |   int x; scanf("%d", &x); return x;
      |          ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define REP(i,n) for (int i=0,_n=(int)(n); i < _n; i++)
template<class T> bool chkmin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool chkmax(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef long long ll;

int nextInt() {
  int x; scanf("%d", &x); return x;
}

int f(string s_org) {
  int res = 0;
  for (int i = 0; i < (int)s_org.size(); ) {
    string s = s_org.substr(i, 2);
    if (s == "IV") { res += 4; i += 2; continue; }
    if (s == "IX") { res += 9; i += 2; continue; }
    if (s == "XL") { res += 40; i += 2; continue; }
    if (s == "XC") { res += 90; i += 2; continue; }
    if (s == "CD") { res += 400; i += 2; continue; }
    if (s == "CM") { res += 900; i += 2; continue; }
    s = s_org.substr(i, 1);
    if (s == "I") { res += 1; i += 1; continue; }
    if (s == "V") { res += 5; i += 1; continue; }
    if (s == "X") { res += 10; i += 1; continue; }
    if (s == "L") { res += 50; i += 1; continue; }
    if (s == "C") { res += 100; i += 1; continue; }
    if (s == "D") { res += 500; i += 1; continue; }
    if (s == "M") { res += 1000; i += 1; continue; }
  }
  return res;
}

string g(int n) {
  string s = "";
  while (n >= 1000) { s += "M"; n -= 1000; }
  if (n >= 500) { s += "D"; n -= 500; }
  if (n >= 400) { s += "CD"; n -= 400; }
  while (n >= 100) { s += "C"; n -= 100; }
  if (n >= 50) { s += "L"; n -= 50; }
  if (n >= 40) { s += "XL"; n -= 40; }
  while (n >= 10) { s += "X"; n -= 10; }
  if (n >= 5) { s += "V"; n -= 5; }
  if (n >= 4) { s += "IV"; n -= 4; }
  while (n >= 1) { s += "I"; n -= 1; }
  return s;
}


int main2() {
  int N = nextInt();
  int ans = 0;
  REP(nnn, N) {
    string s; cin >> s;
    ans += f(s);
  }

  if (ans > 3999)
    cout << "ERROR" << endl;
  else
    cout << g(ans) << endl;

  return 0;
}

int main() {
  for (;!cin.eof();cin>>ws) main2();
  return 0;
}
0