首页 > 其他 > 详细

Integer to Roman

时间:2014-06-17 16:33:44      阅读:289      评论:0      收藏:0      [点我收藏+]

题目

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,布布扣,bubuko.com

Integer to Roman

原文:http://blog.csdn.net/u010378705/article/details/31744511

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!