首页 > 其他 > 详细

Jan 14 - Integer to Roman; String; Integer; Math;

时间:2016-01-15 08:40:54      阅读:226      评论:0      收藏:0      [点我收藏+]
public class Solution {
    public String intToRoman(int num) {
        int[] digits = new int[4];
        int i = 0;
        String s = "";
        digits[i] = num%10;
        while((num = num/10) > 0){
            digits[++i] = num%10;
        }
        for(int j = 0; j <= i; j++){
            if(j == 0){
                switch (digits[j]) {
                    case 1: s = s + "I";
                            break;
                    case 2: s = s + "II";
                            break;
                    case 3: s = s + "III";
                            break;
                    case 4: s = s + "IV";
                            break;
                    case 5: s = s + "V";
                            break;
                    case 6: s = s + "VI";
                            break;
                    case 7: s = s + "VII";
                            break;
                    case 8: s = s + "VIII";
                            break;
                    case 9: s = s + "IX";
                            break;
                    default: break;
                }
            }
            else if(j == 1){
                switch(digits[j]){
                    case 1: s = "X" + s;
                            break;
                    case 2: s = "XX" + s;
                            break;
                    case 3: s = "XXX" + s;
                            break;
                    case 4: s = "XL" + s;
                            break;
                    case 5: s = "L" + s;
                            break;
                    case 6: s = "LX" + s;
                            break;
                    case 7: s = "LXX" + s;
                            break;
                    case 8: s = "LXXX" + s;
                            break;
                    case 9: s = "XC" + s;
                            break;
                    default: break;
                }
            }
            else if(j == 2){
                switch(digits[j]){
                    case 1: s = "C" + s;
                            break;
                    case 2: s = "CC" + s;
                            break;
                    case 3: s = "CCC" + s;
                            break;
                    case 4: s = "CD" + s;
                            break;
                    case 5: s = "D" + s;
                            break;
                    case 6: s = "DC" + s;
                            break;
                    case 7: s = "DCC" + s;
                            break;
                    case 8: s = "DCCC" + s;
                            break;
                    case 9: s = "CM" + s;
                            break;
                    default: break;
                }
            }
            else{
                switch(digits[j]){
                    case 1: s = "M" + s;
                            break;
                    case 2: s = "MM" + s;
                            break;
                    case 3: s = "MMM" + s;
                            break;
                    default: break;
                }
            }
        }
        return s;
    }
}

  

Jan 14 - Integer to Roman; String; Integer; Math;

原文:http://www.cnblogs.com/5683yue/p/5132215.html

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