首页 > 代码库 > Integer to Roman

Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


string intToRoman(int num) {
    int numarr[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
    string strarr[] = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
    string roman;
    int i;
    for(i = 12; i >= 0; i--) {
        while(numarr[i] <= num ) {
            roman.append(strarr[i]);
            num = num - numarr[i];
        }
    }
    return roman;
}


Integer to Roman