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