题目描述:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
本题比较简单,只需要从千位开始到个位判断该integer每一位数值为多少,然后选取对应字符串加入罗马数字中即可。
class Solution {
public:
string intToRoman(int num) {
string one[10]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
string ten[10]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
string hundred[10]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
string thousand[4]={"","M","MM","MMM"};
int digit[4];
string s;
for(int i=0;i<4;i++)
{
digit[i]=num%10;
num/=10;
if(i==0) s=one[digit[i]]+s;
if(i==1) s=ten[digit[i]]+s;
if(i==2) s=hundred[digit[i]]+s;
if(i==3) s=thousand[digit[i]]+s;
}
return s;
}
};