結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
hiyokko2
|
| 提出日時 | 2017-05-28 22:24:37 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,905 bytes |
| コンパイル時間 | 717 ms |
| コンパイル使用メモリ | 90,664 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 15:35:13 |
| 合計ジャッジ時間 | 1,373 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
コンパイルメッセージ
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
ソースコード
//#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;
}
hiyokko2