Number 12th converted to Roman numerals

Source: Internet
Author: User

Roman numerals use seven Roman letters as numbers, i.e. Ⅰ (1), X (10), C (100), M (1000), V (5), L (50), D (500).

At first I followed the 10-way idea, editing different Roman expressions for different intervals, such as 1~9,10~90,100~900,1000~3000

Here's the code:

 Public StaticString Inttoroman (intnum) {                //String Roman = ""; if(num<=0)return""; if(num<=10) {string[] Tens=Newstring[]{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"}; Roman= Roman+tens[num-1]; }        if(10<num && num<100) {//90 or less can be expressed as I V X lstring[] Ten2nines =NewString[] {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};//10,20,30,40,50,60,70,80            intH = ((int) (NUM/10)); intL = num-h*10; Roman= Roman + Ten2nines[h-1] +Inttoroman (L); }                if(100<=num && num<1000) {string[] len=NewString[] {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};//100,200,300,400,500,600,700,800,900            intH = ((int) (num/100)); intL = num-h*100; Roman= Roman + Len[h-1] +Inttoroman (L); }        if(1000<=num && num <4000) {string[] len=NewString[] {"M", "MM", "MMM"}; intH = ((int) (num/1000)); intL = num-h*1000; System.out.println ("H:" +h+ ", L:" +m); Roman= Roman + Len[h-1] +Inttoroman (L); }        returnRoman; }

Later saw online there was a simple, first to find all the Roman numerals of the base, for the decimal, base for (1,2,3,4,5,6,7,8,9,10), the Roman numerals of the base for ("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", " V "," IV "," I "), corresponding number (1000,900,500,400,100,90,50,40,10,9,5,4,1)

Here is the code:

 Public StaticString Inttoroman (intnum) {String data[]= {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; intValue[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}, base =-1; StringBuilder result=NewStringBuilder ();  for(inti = 0;i < data.length;i++){            if((Base=num/value[i])!=0){                 while(Base--! =0) Result.append (Data[i]); Num=num%Value[i]; }        }        returnresult.tostring ();}

Number 12th converted to Roman numerals

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.