Problem:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Converts an Arabic number to a Roman number. Baidu's corresponding I V x L C D m, representing 100,500,100, and then writing a sub-function, entering a number and the corresponding number level, such as Level 1, the value is 4. because it cannot exceed four thousand at most. So you can do the following. Note that the string usage is good. You can simply add it.
Class solution {PRIVATE: String corroman (INT Val, int level) {string base, media, large; string S = ""; if (Level = 1) // single digit {base = "I"; media = "v"; large = "X";} else if (Level = 2) {base = "X "; media = "L"; large = "C";} else if (Level = 3) {base = "C"; media = "D "; large = "M" ;}else {base = "M" ;}if (val = 0) Return "; if (Val <4) {for (INT I = 0; I <val; I ++) S + = base; return s;} If (val = 4) {return base + media ;} if (Val <9) {S = media; For (INT I = 5; I <val; I ++) S + = base; return s ;} return base + large;} public: String inttoroman (INT num) {string s; int A; For (INT I = 4; I> 0; I --) {A = num/POW (10, I-1); num % = (INT) Pow (10, I-1); s + = corroman (A, I );} return s ;}};
In this way, accept is enabled.
Leetcode 12th -- integer to Roman