Integer to Roman

参照Roman to Integer里罗马数字的使用方法,链接如下

http://www.cnblogs.com/waruzhi/p/3332238.html

string intToRoman(int num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function    
        string result;
        int t = num/1000;
        while(t>0){
            t--;
            result += "M";
        }
        num = num - 1000*(num/1000);
        if(num >= 900){
            result += "CM";
            num -= 900;
        }
        if(num >= 500){
            result += "D";
            num -= 500; 
        }
        if(num >= 400){
            result += "CD";
            num -= 400;
        }
        t = num/100;
        while(t > 0){
            t--;
            result += "C";
        }
        num = num - 100*(num/100);
        if(num >= 90){
            result += "XC";
            num -= 90;
        }
        if(num >= 50){
            result += "L";
            num -= 50;
        }
        if(num >= 40){
            result += "XL";
            num -= 40;
        }
        t = num/10;
        while(t > 0){
            t--;
            result += "X";
        }
        num = num - 10*(num/10);
        if(num >= 9){
            result += "IX";
            num -= 9;
        }
        if(num >= 5){
            result += "V";
            num -= 5;
        }
        if(num >= 4){
            result += "IV";
            num -= 4;
        }
        while(num > 0){
            num--;
            result += "I";
        }
        return result;
    }

 

posted on 2013-09-30 11:27  waruzhi  阅读(125)  评论(0编辑  收藏  举报

导航