Translation
给定一个整型数值,将其转换到罗马数字。输入被保证在1到3999之间。
Original
anintegerconvertittoatowithinthefrom1to3999.
I'm not going to tell you at first I was using countless variables and i f ......
And then I just can't stand the fact that so many variables have been put into enumeration, then the next thing is solved.
In order to let everyone understand how the Roman number is counted, here I cut a picture, specific people can use Microsoft Bing Search.
So I put the code on it first:
Public classsolution{ Public string Inttoroman(intNUM) {stringresult =""; Type R =typeof(Roman);foreach(varRinchEnum.getnames (R). Reverse ()) { while(Num >=int. Parse (Enum.format (R, Enum.parse (R, R),"D")) {result + = r.tostring (); num-=int. Parse (Enum.format (R, Enum.parse (R, R),"D")); } }returnResult }} Public enumroman{M = +, CM = the, D = -, CD = -, C = -, XC = -, L = -, XL = +, X =Ten, IX =9, V =5, IV =4, I =1};
Later today I will paste some of the usage of C # enumeration on the blog, do not know the students please pay attention to.
In addition to the use of enumerations, I think you need to be careful to understand these Roman rules, which means remember to add 9 and 4 to the enumeration as well.
So in I NTT oRomaN What are they doing in central?
Match t y p e Span style= "Display:inline-block; width:0px; Height:2.616em; " > And < Span class= "Mrow" id= "mathjax-span-2353" >t y p e o f Span style= "Display:inline-block; width:0px; Height:2.616em; " > New. R
Use < Span class= "Mrow" id= "mathjax-span-2364" >f o r e a c h Span style= "Display:inline-block; width:0px; Height:2.616em; " > Iterate through all the elements in an enumeration
Remember to add ReveRse() , as for why, everybody try not to add to know
If num is larger than the number in the enumeration, the corresponding string (for example, "M") is added to the r e s u l t Span style= "Display:inline-block; width:0px; Height:2.616em; " > In
Finally in num Minus the numbers that have just been used, the numbers are also enumerated to get
Last return Result
OK, so far, ready to meet the next question.
Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp
Leetcode integer to Roman (number of integers to Roman)