結果
問題 | No.518 ローマ数字の和 |
ユーザー |
![]() |
提出日時 | 2017-05-28 23:18:53 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,682 bytes |
コンパイル時間 | 1,570 ms |
コンパイル使用メモリ | 165,716 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-21 15:58:38 |
合計ジャッジ時間 | 2,602 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include<bits/stdc++.h>using namespace std;#define int long longsigned main(){int n;cin >> n;vector<string> r(n);for(int i = 0; i < n; i++)cin >> r[i];int ans = 0;for(int i = 0; i < n; i++){int sum = 0;for(int j = 0; j < r[i].length(); j++){if(r[i][j] == 'I'){if(j < r[i].length() - 1){if(r[i][j + 1] == 'V'){sum += 4;j++;}else if(r[i][j + 1] == 'X'){sum += 9;j++;}else sum++;}else{sum++;}}else if(r[i][j] == 'X'){if(j < r[i].length() - 1){if(r[i][j + 1] == 'L'){sum += 40;j++;}else if(r[i][j + 1] == 'C'){sum += 90;j++;}else sum += 10;}else{sum += 10;}}else if(r[i][j] == 'C'){if(j < r[i].length() - 1){if(r[i][j + 1] == 'D'){sum += 400;j++;}else if(r[i][j + 1] == 'M'){sum += 900;j++;}else sum += 100;}else{sum += 100;}}else if(r[i][j] == 'V')sum += 5;else if(r[i][j] == 'L')sum += 50;else if(r[i][j] == 'D')sum += 500;else sum += 1000;}ans += sum;cerr << sum << endl;}string hundred[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};string ten[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};string one[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};if(ans >= 4000)cout << "ERROR" << endl;else{int a = ans / 1000;if(a != 0)for(int i = 0; i < a; i++)cout << 'M';ans %= 1000;a = ans / 100;if(a != 0)cout << hundred[a - 1];ans %= 100;a = ans / 10;if(a != 0)cout << ten[a - 1];a = ans % 10;if(a != 0)cout << one[a - 1] << endl;}}