Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3
Have you tried attempting the question yourself? If you are stuck, I can think of two ways: • Sum the individual letters: (E.g. IX + XV = XXIV (-1)(10) + (10)(5) = (10)(10)(-1)(5) ) • Or just convert from Roman to decimal then back. (E.g. IX + XV = 9 + 15 = 24 = XXIV)
10th Dec 2018, 11:17 AM
blackcat1111
blackcat1111 - avatar
+ 3
You could go multiple ways about doing that, the simplest way would probably be to convert the values into decimal first and then perform operations on the numbers and convert it back again, there is many examples of how you could convert individual digits into decimal and their complexity depends on the limitations of you program, here's a small example of converting the roman numeral to decimal in C++ #include <iostream> #include <string> #include <map> using namespace std; //Assign each character its decimal value //You can add more numbers and it should work map<char, int> values = {{'I', 1}, {'V',5}, {'X', 10}}; int romanToDecimal(string r) { int total = 0; //Decimal value of the numeral for(int i = 0; i < r.length() - 1; i++) //Loop through the input except last one { //Get value of the current character int v = values[r[i]]; //If the next value is larger, then substract the value, //otherwise add it if(v < values[r[i+1]]) total -= v; else total += v; } //Return the total (plus the last value) return total + values[r.back()]; } Now you can just use that function to convert a roman numeral to decimal. based on that you can make your own implementation of converting decimal to roman to show a result.
10th Dec 2018, 11:27 AM
Edwin Rybarczyk