結果
問題 | No.518 ローマ数字の和 |
ユーザー |
![]() |
提出日時 | 2021-09-20 03:36:29 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,051 bytes |
コンパイル時間 | 611 ms |
コンパイル使用メモリ | 73,152 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-02 08:17:21 |
合計ジャッジ時間 | 1,482 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <iostream>#include <vector>#include <string>using namespace std;const string c = "IVXLCDM";int cn[] = {1, 5, 10, 50, 100, 500, 1000};const vector<string> dc = {"IV", "IX", "XL", "XC", "CD", "CM"};int dcn[] = {4, 9, 40, 90, 400, 900};int dec(const string &r){int res = 0;for(int i = 0; i < r.length(); i++){bool added = false;for(int j = 0; j < 6; j++){if(r.substr(i, 2) == dc[j]){res += dcn[j];added = true;i++;}}if(added) continue;for(int j = 0; j < 7; j++){if(r[i] == c[j]){res += cn[j];}}}return res;}int main(){int n;cin >> n;int total = 0;while(n--){string r;cin >> r;total += dec(r);}if(total >= 4000) cout << "ERROR" << endl;else{string ans = "";if(total >= 1000){for(int i = 0; i < total/1000; i++) ans += "M";}total %= 1000;if(total >= 900) ans += "CM";else if(total >= 500){ans += "D";total -= 500;for(int i = 0; i < total/100; i++) ans += "C";}else if(total >= 400) ans += "CD";else{for(int i = 0; i < total/100; i++) ans += "C";}total %= 100;if(total >= 90) ans += "XC";else if(total >= 50){ans += "L";total -= 50;for(int i = 0; i < total/10; i++) ans += "X";}else if(total >= 40) ans += "XL";else{for(int i = 0; i < total/10; i++) ans += "X";}total %= 10;if(total >= 9) ans += "IX";else if(total >= 5){ans += "V";total -= 5;for(int i = 0; i < total; i++) ans += "I";}else if(total >= 4) ans += "IV";else{for(int i = 0; i < total; i++) ans += "I";}cout << ans << endl;}return 0;}