diff --git a/solutions/cpp/roman-numerals/1/roman_numerals.cpp b/solutions/cpp/roman-numerals/1/roman_numerals.cpp new file mode 100644 index 0000000..f9094ba --- /dev/null +++ b/solutions/cpp/roman-numerals/1/roman_numerals.cpp @@ -0,0 +1,31 @@ +#include "roman_numerals.h" +#include +#include + +using namespace std; + +namespace roman_numerals { + +string convert(int number) { + string roman = ""; + + // Diccionario estático de equivalencias + const vector> roman_mapping = { + {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"} + }; + + // Algoritmo Greedy + for (const auto& mapping : roman_mapping) { + while (number >= mapping.first) { + roman += mapping.second; // Concatenamos el símbolo + number -= mapping.first; // Reducimos el valor del número + } + } + + return roman; +} + +} diff --git a/solutions/cpp/roman-numerals/1/roman_numerals.h b/solutions/cpp/roman-numerals/1/roman_numerals.h new file mode 100644 index 0000000..2338dd1 --- /dev/null +++ b/solutions/cpp/roman-numerals/1/roman_numerals.h @@ -0,0 +1,9 @@ +#pragma once +#include + +namespace roman_numerals { + + // Traduce un entero arábigo a su equivalente romano + std::string convert(int number); + +}