From d88b264bf0317cbbcb0dcbf72164f64147a5bd16 Mon Sep 17 00:00:00 2001 From: hard02 <211b126@juetguna.in> Date: Thu, 11 May 2023 00:10:14 +0530 Subject: [PATCH] Roman-Integer files added --- algorithms/CPlusPlus/roman-integer.cpp | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 algorithms/CPlusPlus/roman-integer.cpp diff --git a/algorithms/CPlusPlus/roman-integer.cpp b/algorithms/CPlusPlus/roman-integer.cpp new file mode 100644 index 00000000..44a9db6e --- /dev/null +++ b/algorithms/CPlusPlus/roman-integer.cpp @@ -0,0 +1,70 @@ +#include +#include + +using namespace std; + +string intToRoman(int num) { + // Define the symbols and their corresponding values + unordered_map symbol_map = { + {1000, "M"}, + {900, "CM"}, + {500, "D"}, + {400, "CD"}, + {100, "C"}, + {90, "XC"}, + {50, "L"}, + {40, "XL"}, + {10, "X"}, + {9, "IX"}, + {5, "V"}, + {4, "IV"}, + {1, "I"} + }; + + // Build the Roman numeral string + string roman = ""; + for (auto& [value, symbol] : symbol_map) { + while (num >= value) { + roman += symbol; + num -= value; + } + } + return roman; +} + +int romanToInt(string s) { + // Define the symbols and their corresponding values + unordered_map symbol_map = { + {'M', 1000}, + {'D', 500}, + {'C', 100}, + {'L', 50}, + {'X', 10}, + {'V', 5}, + {'I', 1} + }; + + // Calculate the integer value + int value = 0; + for (int i = 0; i < s.length(); i++) { + if (i > 0 && symbol_map[s[i]] > symbol_map[s[i-1]]) { + value += symbol_map[s[i]] - 2 * symbol_map[s[i-1]]; + } else { + value += symbol_map[s[i]]; + } + } + return value; +} + +int main() { + int num = 1234; + string roman = intToRoman(num); + cout << num << " in Roman numerals is " << roman << endl; + + string s = "MCCXXXIV"; + int value = romanToInt(s); + cout << s << " in integer value is " << value << endl; + + return 0; +} +