Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
没认真想,看到一种方法,应该算是最简单的方法了吧
基础知识:
罗马数字是由字符I,V,X,L,C,D,M等等表示的,其中
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
接下来应该是V开始的重复,但是上面要加一个横线,表示对应数字的1000倍。
个位应该是:I,II,III,IV,V,VI,VII,VIII,IX
十位应该是:X,XX,XXX,XL,L,LX,LXX,LXXX,XC
百位应该是:C,CC,CCC,CD,D,DC,DCC,DCCC,CM
class Solution { public: string intToRoman(int num) { char c[10][10][10]={{"0","I","II","III","IV","V","VI","VII","VIII","IX"},{"0","X","XX","XXX","XL","L","LX" ,"LXX","LXXX","XC"},{"0","C","CC","CCC","CD","D", "DC","DCC","DCCC","CM"},{"0","M","MM","MMM"}}; int t=1; int tmp=num; string st; if(tmp/1000!=0) st+=c[3][tmp/1000]; if((tmp%1000)/100!=0) st+=c[2][(tmp%1000)/100]; if((tmp%100)/10!=0) st+=c[1][(tmp%100)/10]; if(tmp%10!=0) st+=c[0][tmp%10]; return st; } };