012 Integer to Roman (Java)

Source: Internet
Author: User

Given an integer, convert it to a Roman numeral.

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

First to understand the composition of the Roman number

basic word Character i v x l c d m
The corresponding Arabic numerals are expressed as 1 5 10 50 100 500 1000

    1. The same number ligatures, the number represented is equal to the number of these numbers added, such as: ⅲ=3;
    2. The small number on the right side of the large number, the number represented is equal to the number of these numbers added, such as: Ⅷ=8, ⅻ=12;
    3. Small numbers, (limited to Ⅰ, X, and C) on the left side of large numbers, the number represented is equal to the number of large reduction numbers obtained, such as: ⅳ=4, ⅸ=9;
    4. When normal use, the number of ligatures should not exceed three times;
    5. Draw a horizontal line over a number, indicating that the number is enlarged by 1000 times times.
Number of groups ruleEditThere are a few notes to be mastered:
    1. The basic number Ⅰ, X, C any one, the number of their own use, or put on the right side of the large number of the composition, not more than three; on the left of the large number can only be used one;
    2. It is not possible to use any one of the basic digits V, L, D as decimals on the left side of the large number to make up the number by subtracting the method;
    3. The small numbers on the left of V and X can only be used Ⅰ;
    4. The small numbers on the left of L and C can only be used with X;
    5. The small numbers on the left of D and M can only be used in C.
Contrast exampleEdit• single Digit exampleⅠ-1, Ⅱ-2, ⅲ-3, ⅳ-4, ⅴ-5, ⅵ-6, ⅶ-7, Ⅷ-8, ⅸ-9• Example of 10 digitsⅩ-10, ⅺ-11, ⅻ-12, XIII-13, XIV-14, XV-15, XVI-16, XVII-17, XVIII-18, XIX-19, XX-20, XXI-21, XXII-22, XXIX-29, XXX-30, XXXIV-34, XXXV-35, XXXIX-39, XL-40, L-50, LI-51, LV-55, LX-60, LXV-65, LXXX-80, XC-90, XCIII-93, XCV-95, XCVIII-98, XCIX-99• Examples of hundredC-100, CC-200, CCC-300, CD-400, D-500, DC-600, DCC-700, DCCC-800, CM-900, CMXCIX-999• Examples of thousandsM-1000, MC-1100, MCD-1400, MD-1500, MDC-1600, MDCLXVI-1666, MDCCCLXXXVIII-1888, MDCCCXCIX-1899, MCM-1900, MCMLXXVI-1976 , MCMLXXXIV-1984, MCMXC-1990, MM-2000, MMMCMXCIX-3999 reference http://baike.baidu.com/link?url= ikdrmd-342dsxb2crvwmakofiecsd1nx8i4oaznws87wlmvqzikgf-clozquhe3avzvjetr88ju2zw4vvf4k3a

Then the code is as follows

Public  string Getstr (String str,int i,string s1,string s2,string S3) {switch (i) {case 1:str+=s3;break;case 2:STR+=S3 +s3;break;case 3:str+=s3+s3+s3;break;case 4:str+=s3+s2;break;case 5:str+=s2;break;case 6:str+=s2+s3;break;case 7: Str+=s2+s3+s3;break;case 8:str+=s2+s3+s3+s3;break;case 9:str+=s3+s1;break;} return str;}    Public  String inttoroman (int num) {    int ge,shi,bai,qian;    String string= "";    if ((qian=num/1000)!=0) {    num=num%1000;    Switch (Qian) {case    3:string+= "MMM";    Case 2:string+= "MM";    Case 1:string+= "M";    }    }    if ((bai=num/100)!=0) {    num=num%100;    String=getstr (String,bai, "M", "D", "C");    }    if ((SHI=NUM/10)!=0) {    num=num%10;    String=getstr (String,shi, "C", "L", "X");    }    if ((ge=num)!=0) {    string=getstr (string,ge, "X", "V", "I");    }    return string;    }


012 Integer to Roman (Java)

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.