#include <string> #include <vector> #include <iostream> #include <ctype.h> using namespace std; vector<char> nums; vector<string> Val; vector<int> nums2; int GetCount(string arr, string a) { int c = 0; if (arr.size() <= a.size()) return 0; for (int i = 0; i < arr.length() - a.length()-1; i++) if (arr.substr(i, a.length()) == a) c++; return c; } int GetIndex(vector<char> d, char a) { for (int i = 0; i < d.size(); i++) if (d[i] == a) return i; return -1; } int GetIndex2(vector<string> d, string a) { for (int i = 0; i < d.size(); i++) if (d[i] == a) return i; return -1; } bool validate1(string num) { if (num == "") return true; if (GetCount(num, "iiii") == 0) if (GetCount(num, "xxxx") == 0) if (GetCount(num, "cccc") == 0) if (GetCount(num, "mmmm") == 0) if (GetCount(num, "vv") == 0) if (GetCount(num, "ll") == 0) if (GetCount(num, "dd") == 0) if (GetCount(num, "om") == 0) return true; return false; } bool IsLarger(char a, char b) { if (GetIndex(nums, a) < GetIndex(nums, b)) return true; return false; } bool validate2(string comp) { if (comp == "") return true; for (int i = 0; i < comp.length() - 1; i++) if (IsLarger(comp[i], comp[i + 1]) == true) if ((comp[i] == 'i' || comp[i] == 'x' || comp[i] == 'c') && GetIndex2(Val, comp.substr(i, 2)) == -1)return false; return true; } int GetResult(char a, char b) { return (nums2[GetIndex(nums, b)] - nums2[GetIndex(nums, a)]); } void pre() { nums.push_back('i'); nums.push_back('v'); nums.push_back('x'); nums.push_back('l'); nums.push_back('c'); nums.push_back('d'); nums.push_back('m'); nums2.push_back(1); nums2.push_back(5); nums2.push_back(10); nums2.push_back(50); nums2.push_back(100); nums2.push_back(500); nums2.push_back(1000); Val.push_back("iv"); Val.push_back("ix"); Val.push_back("xl"); Val.push_back("xc"); Val.push_back("cm"); Val.push_back("cd"); } int completeSolve(string num) { if (num == "") return 0; int res = 0; for (int i = 0; i < num.size(); i++) { if (num[i] == 'o') res *= 1000; else { if (GetIndex2(Val, num.substr(i, 2)) == -1) { res += nums2[GetIndex(nums, num[i])]; } else { res += GetResult(num[i], num[i + 1]); i++; } } } return res; } string genr(int intnum) { string res; if (intnum > 4999) { res = genr((int)(intnum / 1000)) + "O"; intnum %= 1000; } int m, d, c, l, x, v, i, n; m = intnum / 1000; d = ((intnum % 1000) / 500); c = ((intnum % 500) / 100); l = ((intnum % 100) / 50); x = ((intnum % 50) / 10); v = ((intnum % 10) / 5); i = (intnum % 5); n = m + d + c + l + x + v + i; while (n > 0){ for (m; m > 0; m--) res = res + "M"; for (d; d > 0; d--) res = res + "D"; if (c == 4) { res = res + "CD"; c = 0; } else for (c; c > 0; c--) res = res + "C"; for (l; l>0; l--) res= res + "L"; if (x == 4) {res = res + "XL"; x = 0;} else for (x; x>0; x--) res= res + "X"; for (v; v>0; v--) res= res + "V"; if (i == 4) {res = res + "IV"; i = 0; } else for (i; i>0; i--) res= res + "I"; n--;} return res; } int Doew(string al){ string res=""; string al2= ""; int plus = -1; for (int i = 0; i < al.size(); i++) al2 += tolower(al[i]); for (int i = 0; i < al2.size(); i++) if (al2[i] == '+') { plus = i; break; } string f1="", f2=""; if (al2[0] != '+') f1 = al2.substr(0, plus); if (al2[al2.length()] != '+') f2 = al2.substr(plus+1); if (plus == -1) {cout<< al2 << "=" << completeSolve(al2); return 0;} if (validate1(f1) && validate2(f1) && validate1(f2)&& validate2(f2)) { int res = completeSolve(f1); res += completeSolve(f2); std::cout << genr(res) << "=" << res; } else std::cout << "INVALID"; cout << endl; return 0; } int main(){ pre(); // while (true){ string inp; cin >> inp; Doew(inp); // } return 0;} |
Double click to view unformatted code.