**title:** Arabic numerals to Roman numerals

Given an integer, convert it to a Roman numeral.

Input is guaranteed to being within the range from 1 to 3999.

Original title link address: https://leetcode.com/problems/integer-to-roman/

Analysis: Test instructions Arabic numerals num to roman numerals

**Spelling Rules**

There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000). Any positive integer can be represented by the following rules. It is important to note that there is no "0" in the Roman numerals, regardless of the rounding system. Roman numerals are generally considered to be used only for counting, not for calculation.

Repeat several times: a Roman number repeats several times, indicating several times the number.

Right plus left minus:

The smaller Roman numerals on the right side of the larger Roman numerals indicate large numbers plus small numbers.

The smaller Roman numerals on the left of the larger Roman numerals indicate that large numbers decrease the numbers.

The left-minus number is limited to I, X, and C. For example, 45 can not be written as VL, only XLV

However, the left subtraction cannot span a single digit. For example, 99 may not be represented by an IC (100-1), but rather by XCIX ([100-10] + [10-1]). (equivalent to each digit of the Arabic numerals.) ）

The left minus number must be one, such as 8 written in VIII, not IIX.

The right plus number cannot be more than three consecutive digits, such as 14 for XIV rather than XIIII. (See "Digital Restrictions" below.) ）

Add line by thousand:

By adding a horizontal line or a subscript to the Roman numerals, it means multiplying this number by 1000, which is 1000 times times the original number.

Similarly, if there are two horizontal lines above, it is 1000000 (1000^{2}) times the original number.

Digital Restrictions:

The same digital can only appear at most three times, such as 40 can not be represented as XXXX, but to be represented as XL.

Exception: Since IV is the first word of the Roman mythology Lord Jupiter (i.e. Ivpiter, the Roman alphabet does not have J and u), it is sometimes substituted with IIII for IV

Wiki excerpt:

https://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97

Solution One: The number of NUM, 10, hundred, thousands on the numbers, in turn for each to judge.

Java code: (accepted)

` Public class integetoroman { /** * @param args * * Public Static void Main(string[] args) {//TODO auto-generated method stub intnum = -; System.out.println ("Int to Roman:"+ Inttoroman (num)); } Public StaticStringInttoroman(intNUM) {intt = num/ +;//save the Thousand intH = (NUM-T * +) / -;//save The Hundred intD = (NUM-T * +-H * -) /Ten;//save the decimal intU = num-t * +-H * --D *Ten;//Save the unitString romanstring ="";//calculate the Thousand for(inti =0; i < T;i + +) romanstring = romanstring +' M ';//calculate The Hundred if( -<= h * -&& h * -<= -){ for(inti =0; i < H;i + +) romanstring = romanstring +' C '; }Else if(H * -== -) {romanstring = romanstring +"CD"; }Else if( -<= h * -&& h * -<= -) {romanstring = romanstring +' D '; for(inti =0; I < h%5; i + +) romanstring = romanstring +' C '; }Else if(H * -== the) {romanstring = romanstring +"CM"; }//calculate the decimal if(Ten<= d *Ten&& d *Ten<= -){ for(inti =0; i < D;i + +) romanstring = romanstring +' X '; }Else if(d *Ten== +) {romanstring = romanstring +"XL"; }Else if( -<= d *Ten&& d *Ten<= the) {romanstring = romanstring +' L '; for(inti =0; I < D%5; i + +) romanstring = romanstring +' X '; }Else if(d *Ten== -) {romanstring = romanstring +"XC"; }//calculate the Unit if(1<= u && u <=3){ for(inti =0; i < U;i + +) romanstring = romanstring +' I '; }Else if(U = =4) {romanstring = romanstring +"IV"; }Else if(5<= u && u <=8) {romanstring = romanstring +' V '; for(inti =0; I < U%5; i + +) romanstring = romanstring +' I '; }Else if(U = =9) {romanstring = romanstring +"IX"; }returnromanstring; }}`

Test results:

`Intto Roman: LIntto Roman: DIIntto Roman: MMMCMXCIXIntto Roman: MMCXXXIV`

Solution Two:

All the small numbers in the previous combination as a basic number, and then make a corresponding table of values can solve the problem.

Reference: http://blog.csdn.net/fightforyourdream/article/details/12934139

Java code:

` Public class integetoroman { /** * @param args * * Public Static void Main(string[] args) {//TODO auto-generated method stub intnum = -; System.out.println ("Int to Roman:"+ Inttoroman (num)); } Public StaticStringInttoroman(intNUM) {//other Solution if(Num <1|| num >3999){return "-1"; }int[] Aarray = { +, the, -, -, -, -, -, +,Ten,9,5,4,1}; String[] Rarray = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; String Rnumber =""; for(intI=0; i<aarray.length; i++) { while(Num >= aarray[i]) {Rnumber + = Rarray[i]; num-= Aarray[i]; } }returnRnumber; }}`

The relevant code is placed in the personal GITHUB:HTTPS://GITHUB.COM/GANNYEE/LEETCODE/TREE/MASTER/SRC

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Personal GitHub code space: Https://github.com/gannyee

Leetcode Problem Solving report--integer to Roman