Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
answer:
class Solution {
public:
string intToRoman(int num) {
string thousand[] = {"","M","MM","MMM"};
string hundred[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
string ten[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
string single[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
int index = 0, mod = 0;
string result = "";
index = num / 1000;
result += thousand[index];
mod = num % 1000;
index = mod / 100;
result += hundred[index];
mod = mod % 100;
index = mod /10;
result += ten[index];
mod = mod % 10;
index = mod % 10;
result += single[index];
return result;
}
};