Given an integer, convert it to a Roman numeral.
Input is guaranteed to being within the range from 1 to 3999.
Analysis: This problem reference leetcode.pdf solution, code concise, processing ingenious. The following points are worth reference:
(1) 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 as the base, thus eliminating the need to handle special cases such as 900,400,90,40,9,4 alone.
(2) It is easier to calculate from a large base, since there are only 13 bases and the maximum value is 3999, this method is feasible.
classSolution { Public: stringInttoroman (intnum) { intRadix[] = { +, the, -, -, -, -, -, +,Ten,9,5,4,1}; stringSymbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; stringresult; for(inti =0; num >0; i++){ intCount = num/Radix[i]; Num%=Radix[i]; while(Count >0) {result+=Symbol[i]; Count--; } } returnresult; }};
Leetcode:integer to Roman