結果
問題 | No.518 ローマ数字の和 |
ユーザー |
![]() |
提出日時 | 2017-05-28 22:49:53 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,397 bytes |
コンパイル時間 | 1,374 ms |
コンパイル使用メモリ | 160,368 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 15:42:59 |
合計ジャッジ時間 | 2,167 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h> #define rep(i, a, n) for(int i = a; i < n; i++) #define repb(i, a, b) for(int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long #define fi first #define se second using namespace std; typedef pair<int, int> P; const int INF = 1e8; int d[7] = {1, 5, 10, 50, 100, 500, 1000}; string str = "IVXLCDM"; int change_10(string s){ int res = 0; int now, pre = INF; rep(i, 0, s.size()){ int id = str.find(s[i]); now = d[id]; if(pre < now ){ res += now - pre; pre = INF; }else{ if(pre != INF) res += pre; pre = now; } } if(pre != INF) res += pre; return res; } signed main(){ int n; cin >> n; int sum = 0; rep(i, 0, n){ string s; cin >> s; int tmp = change_10(s); sum += tmp; // cout << tmp << endl; } if(sum >= 4000){ cout << "ERROR" << endl; return 0; } string ans = ""; if(sum >= 1000){ rep(i, 0, sum / 1000) ans += str[6]; sum %= 1000; } if(sum >= 100){ int q = sum / 100; sum %= 100; if(q == 9) ans += "CM"; else if(q == 4) ans += "CD"; else{ if(q >= 5) ans += "D"; rep(i, 0, q % 5) ans += "C"; } } if(sum >= 10){ int q = sum / 10; sum %= 10; if(q == 9) ans += "XC"; else if(q == 4) ans += "XL"; else{ if(q >= 5) ans += "L"; rep(i, 0, q % 5) ans += "X"; } } if(sum >= 1){ int q = sum; if(q == 9) ans += "IX"; else if(q == 4) ans += "IV"; else{ if(q >= 5) ans += "V"; rep(i,0 , q % 5) ans += "I"; } } // repb(i, 6, 0){ // if(sum < d[i]) continue; // int q = sum / d[i]; // sum %= d[i]; // // cout <<d[i] << " " << q << " "<< sum << endl; // string tmp = ""; // // if(q == 4){ // // tmp += str[i]; // // tmp += str[i + 1]; // // // cout << 1 << " " << tmp << endl; // // }else{ // rep(j, 0, q){ // tmp += str[i]; // } // // } // // cout << tmp << endl; // ans += tmp; // } cout << ans << endl; }