1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <bits/stdc++.h> #define ouo ios_base::sync_with_stdio(false), cin.tie(0) #define ll long long #define db double using namespace std;
unordered_map<char, int> mp = { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
unordered_map<int, char> re_mp = { {1, 'I'}, {5, 'V'}, {10, 'X'}, {50, 'L'}, {100, 'C'}, {500, 'D'}, {1000, 'M'}};
int int_mp[7] = {1, 5, 10, 50, 100, 500, 1000};
string s1, s2;
int solve(string s) { int n = mp[s[0]]; for (int i = 1; i < s.length(); i++) { n += mp[s[i]]; if (mp[s[i]] > mp[s[i - 1]]) { n -= mp[s[i - 1]] * 2; } } return n; }
int main() { ouo;
while (cin >> s1) { if (s1 == "#") break; cin >> s2;
int n1 = solve(s1); int n2 = solve(s2); int n3 = abs(n1 - n2); if (n3 == 0) { cout << "ZERO\n"; continue; }
deque<char> ans; int power = 0; while (n3 != 0) { int tmp = (n3 % 10) * pow(10, power); switch (tmp) { case 4: ans.push_front(re_mp[tmp + 1]); ans.push_front(re_mp[1]); break; case 9: ans.push_front(re_mp[tmp + 1]); ans.push_front(re_mp[1]); break; case 40: ans.push_front(re_mp[tmp + 10]); ans.push_front(re_mp[10]); break; case 90: ans.push_front(re_mp[tmp + 10]); ans.push_front(re_mp[10]); break; case 400: ans.push_front(re_mp[tmp + 100]); ans.push_front(re_mp[100]); break; case 900: ans.push_front(re_mp[tmp + 100]); ans.push_front(re_mp[100]); break; default: deque<char> str_tmp; for (int i = 6; i >= 0; i--) { while (tmp / int_mp[i] > 0) { str_tmp.push_back(re_mp[int_mp[i]]); tmp -= int_mp[i]; } } for (int i = str_tmp.size() - 1; i >= 0; i--) { ans.push_front(str_tmp[i]); } break; } power++; n3 /= 10; } for (auto i : ans) { cout << i; } cout << "\n"; } return 0; }
|