diff options
Diffstat (limited to '')
-rw-r--r-- | 13/main.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/13/main.cpp b/13/main.cpp new file mode 100644 index 0000000..e306587 --- /dev/null +++ b/13/main.cpp @@ -0,0 +1,34 @@ + +#include "header.hpp" +#include <map> + +class Solution { +public: + Solution() = default; + static int romanToInt(std::string s) { + std::map<unsigned char, int32_t> romanNumber = { + {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, + {'C', 100}, {'D', 500}, {'M', 1000}}; + int result = 0; + int val = 0; + unsigned char prev; + + for (int i = 0; i < s.length() - 1; ++i) { + val = romanNumber[s[i]]; + if (romanNumber[s[i]] < romanNumber[s[i + 1]]) { + result = result - val; + } else { + result = result + val; + } + } + + return result + romanNumber[s[s.length() - 1]]; + } +}; + +int main(int argc, char **argv) { + std::string s; + std::cin >> s; + std::cout << Solution::romanToInt(s) << "\n"; + return 0; +} |