题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
借鉴了别人的思路
知识点:罗马数字的表达,字符串合并
代码如下(C++):
class Solution {
public:
string intToRoman(int num) {
//罗马数字的基本型为:I=1,V=5,X=10,L=50,C=100,D=500,M=1000,相同的罗马数字最多不能超过三个。所以对于4只能表示为5-1即IV,左减右加。
//同理,9=IX,40=XL,90=XC,400=CD,900=CM。根据这些基本型,
string thousand[4]={"","M","MM","MMM"};
string hundred[11]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
string ten[11]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
string one[11]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
string result="";
string* trans[4]={one,ten,hundred,thousand};
int index=0;
while(num!=0)
{
int key=num%10;
result=trans[index][key]+result; //加号前后的两个东西顺序不能变
num=num/10;
index++;
}
return result;
}
};