码迷,mamicode.com
首页 > 其他好文 > 详细

Integer to Roman

时间:2019-03-13 12:02:38      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:none   border   tor   cas   clear   要求   one   href   efault   

题目

Given an integer, convert it to a roman numeral.

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

方法

/*语法:
*(1)基本数字I,X,C中的不论什么一个连用构成数目,都不能超过三个;
* ?放在大数的左边仅仅能用一个。


?(2)基本数字V ,L ,D 中的不论什么一个放在大数的右边採用相加的方式构成数目,仅仅能使用一个。


?(3)V 和X 左边的小数字仅仅能用I。


?(4)L 和C 左边的小数字仅仅能用X。


?(5)D 和M 左边的小数字仅仅能用C 。


?(6)在数字上加一横表示这个数字的1000倍。【因难以实现输入。临时忽略此要求】*/

	    public String intToRoman(int num) {
	        int digit;
	        String str = "";
	        int n = 10;
	        while(num > 0){
	            digit = num % n;
	            switch(n){
	                case 10:
	                    switch(digit){
	                        case 1:
	                            str = "I" + str;
	                            break;
	                        case 2:
	                            str = "II" + str;
	                            break;
	                        case 3:
	                            str = "III" + str;
	                            break;
	                        case 4:
	                            str = "IV" + str;
	                            break;
	                        case 5:
	                            str = "V" + str;
	                            break;
	                        case 6:
	                            str = "VI" + str;
	                            break;     
	                        case 7:
	                            str = "VII" + str;
	                            break;
	                        case 8:
	                            str = "VIII" + str;
	                            break;   
	                        case 9:
	                            str = "IX" + str;
	                            break;     
	                        default:
	                            break;
	                    }
	                    break;
	                case 100:
	                    digit = digit / 10;
	                    switch(digit){
	                        case 1:
	                            str = "X" + str;
	                            break;
	                        case 2:
	                            str = "XX" + str;
	                            break;
	                        case 3:
	                            str = "XXX" + str;
	                            break;
	                        case 4:
	                            str = "XL" + str;
	                            break;
	                        case 5:
	                            str = "L" + str;
	                            break;
	                        case 6:
	                            str = "LX" + str;
	                            break;     
	                        case 7:
	                            str = "LXX" + str;
	                            break;
	                        case 8:
	                            str = "LXXX" + str;
	                            break;   
	                        case 9:
	                            str = "XC" + str;
	                            break;   
	                        default :break;
	                    }
	                    break;
	                case 1000:
	                    digit = digit / 100;
	                    switch(digit){
	                        case 1:
	                            str = "C" + str;
	                            break;
	                        case 2:
	                            str = "CC" + str;
	                            break;
	                        case 3:
	                            str = "CCC" + str;
	                            break;
	                        case 4:
	                            str = "CD" + str;
	                            break;
	                        case 5:
	                            str = "D" + str;
	                            break;
	                        case 6:
	                            str = "DC" + str;
	                            break;     
	                        case 7:
	                            str = "DCC" + str;
	                            break;
	                        case 8:
	                            str = "DCCC" + str;
	                            break;   
	                        case 9:
	                            str = "CM" + str;
	                            break;  
	                        default :
	                            break;
	                    }
	                    break;
	                case 10000:
	                    digit = digit / 1000;
	                    switch(digit){
	                        case 1:
	                            str = "M" + str;
	                            break;
	                        case 2:
	                            str = "MM" + str;
	                            break;
	                        case 3:
	                            str = "MMM" + str;
	                            break;
	                  
	                    }
	                    break;
	            }
	            num = num - digit * n/10;
	            n = n * 10;
	        }
	        return str;
	    }


Integer to Roman

标签:none   border   tor   cas   clear   要求   one   href   efault   

原文地址:https://www.cnblogs.com/mqxnongmin/p/10522166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!