Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions solutions/cpp/roman-numerals/1/roman_numerals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "roman_numerals.h"
#include <vector>
#include <utility>

using namespace std;

namespace roman_numerals {

string convert(int number) {
string roman = "";

// Diccionario estático de equivalencias
const vector<pair<int, string>> 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;
}

}
9 changes: 9 additions & 0 deletions solutions/cpp/roman-numerals/1/roman_numerals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <string>

namespace roman_numerals {

// Traduce un entero arábigo a su equivalente romano
std::string convert(int number);

}