Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
public:
string intToRoman(int num) {
stringstream oss;
oss << digitSymbol(num/1000%10, 0, 0, 'M');
oss << digitSymbol(num/100%10, 'M', 'D', 'C');
oss << digitSymbol(num/10%10, 'C', 'L', 'X');
oss << digitSymbol(num%10, 'X', 'V', 'I');
return oss.str();
}
string digitSymbol(int num, char ten, char five, char one)
{
stringstream oss;
if(9 == num)
{
oss << one;
oss << ten;
}
else if(num >= 5)
{
oss << five;
while(num-- > 5)
oss << one;
}
else if( 4== num)
{
oss << one;
oss << five;
}
else if(num < 4 && num > 0)
{
while(num--)
oss << one;
}
return oss.str();
}
};