結果

問題 No.518 ローマ数字の和
ユーザー hiyokko2hiyokko2
提出日時 2017-05-28 22:24:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,905 bytes
コンパイル時間 814 ms
コンパイル使用メモリ 92,424 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:20:25
合計ジャッジ時間 1,701 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 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 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 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 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void intToRoman(long long int, char*)’:
main.cpp:67:18: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                  ^~~
main.cpp:67:22: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                      ^~~~
main.cpp:67:27: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                           ^~~
main.cpp:67:31: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                               ^~~~
main.cpp:67:36: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                                    ^~~
main.cpp:67:40: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                                        ^~~~
main.cpp:67:45: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                                             ^~~
main.cpp:67:49: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   67 |     char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
      |                                                 ^~~~
main.cpp:67:54: warning: ISO C++ forbids converting a string constant

ソースコード

diff #

//#define LOCAL

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <cstring>

#define int long long
//typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); i++)

using namespace std;

int N;
string R[105];
map<char, int> mp;

int romanToInt(string roman)
{
    mp['I'] = 1;
    mp['V'] = 5;
    mp['X'] = 10;
    mp['L'] = 50;
    mp['C'] = 100;
    mp['D'] = 500;
    mp['M'] = 1000;

    int now = 0;
    int ret = 0;

    while (now < roman.length()) {
        string two = roman.substr(now, 2);
        if (two == "IV") {
            ret += 4;
            now += 2;
        } else if (two == "IX") {
            ret += 9;
            now += 2;
        } else if (two == "XL") {
            ret += 40;
            now += 2;
        } else if (two == "XC") {
            ret += 90;
            now += 2;
        } else if (two == "CD") {
            ret += 400;
            now += 2;
        } else if (two == "CM") {
            ret += 900;
            now += 2;
        } else {
            ret += mp[roman[now]];
            now += 1;
        }
    }

    return ret;
}

void intToRoman(int num, char *roman)
{
    int a[13]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
    char *r[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
    roman[0]='\0';
    for(int i=0;i<13;i++) {
        while(num>=a[i]){
            strcat(roman,r[i]);
            num-=a[i];
        }
    }
}

signed main()
{
#ifdef LOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif

    cin >> N;
    int ans = 0;
    rep(i, N) {
        cin >> R[i];
        ans += romanToInt(R[i]);
    }

    if (ans > 3999) {
        cout << "ERROR" << endl;
    } else {
        char aaannss[50];
        intToRoman(ans, aaannss);
        cout << aaannss << endl;
    }

    //cout << romanToInt("CCCXL") << endl;
}
0