12345678910111213141516171819202122232425262728 |
- /* Create a function that takes an Arabic number and converts
- * it into a Roman number.
- * All roman numerals should be returned as uppercase.
- * The largest number that can be represented in this notation is 3,999.
- */
- // 1 = I, 4 = IV, 5 = V, 6 = VI, 9 = IX,
- // 10 = X, 50 = L , 100 = C , 500 = D , 1000 = M
- std::string convertToRoman(int num) {
- // array of int corresponding to roman numeral values
- int val[13] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
- std::vector<std::string> roman = {"I","IV","V","IX","X","XL",
- "L","XC","C","CD","D","CM","M"};
- std::string r = "";
- int i = roman.size()-1;
- // starting with the biggest number, populate "r" with equivalent roman numeral
- while(num>0){
- // how many times to add the current letter(s)
- int fixIt = num/val[i];
- // add the current leter(s) to the string
- while(fixIt--){
- r += roman[i];
- }
- // replace the previous value of num with remainder
- num = num%val[i];
- i--;
- }
- return r;
- }
|