Leetcode integer to Roman (number of integers to Roman)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.