Last active
August 13, 2017 13:50
-
-
Save Dimanaux/86042be74632262282a8ee03b2e5f0d7 to your computer and use it in GitHub Desktop.
there is a function that converts numbers from decimal to roman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <map> | |
#include <string> | |
std::string decimalToRoman(int n) { | |
std::string roman = ""; | |
const std::map<int, std::string> rd = { | |
{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"}, | |
{0, "0"} // IS NEVER USED | |
}; | |
// doing like this because of sorted structure of std::map, { 0: "0" } too | |
for (auto it = end(rd); it != begin(rd); --it) { | |
while (n >= (*it).first) { | |
n -= (*it).first, | |
roman += (*it).second; | |
} | |
} | |
return roman; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment