美文网首页程序员
LeetCode12: Integer to Roman

LeetCode12: Integer to Roman

作者: Jesson3264 | 来源:发表于2018-05-13 10:47 被阅读0次

    题目链接:https://leetcode.com/problems/integer-to-roman/description/

    这是我写过最长的 switch case.
    题目的意思大概是:将十进制数转为罗马形式。
    思路:每一个十进制位用一个罗马字母表示,最大的数是3999,所以只要把一个数拆成每一位的数相加就兴。举个例子:
    比如要求:3789, 拆成 300+700+80+9 就可以了,
    然后switch case 每个数字就行了。 有什么更好到方法缩短代码吗?

    class Solution{
    public:
        string intToRoman(int num) {
            string retStr;
            int tNextNum = 0;
            tNextNum = getNextNum(num);
            while (tNextNum)
            {
                //handle
    //            cout<<"num:"<<tNextNum<<endl; 
                num  -= tNextNum;
                //编码
                switch (tNextNum)
                {
                    case 1:
                        retStr.push_back('I');
                        break;       
                        case 2:
                        retStr.push_back('I');
                        retStr.push_back('I');
                        break;
                    case 3:
                        retStr.push_back('I');
                        retStr.push_back('I');
                        retStr.push_back('I');
                        break;
                    case 4:
                        retStr.push_back('I');
                        retStr.push_back('V');
                        break;
                    case 5:
                        retStr.push_back('V');
                        break;
                    case 6:
                        retStr.push_back('V');
                        retStr.push_back('I');
                        break;
                    case 7:
                        retStr.push_back('V');
                        retStr.push_back('I');
                        retStr.push_back('I');
                        break;
                    case 8:
                        retStr.push_back('V');
                        retStr.push_back('I');
                        retStr.push_back('I');
                        retStr.push_back('I');
                        break;
                    case 9:
                        retStr.push_back('I');
                        retStr.push_back('X');
                        break;
                     case 10:
                        retStr.push_back('X');
                        break;
                    case 20:
                        retStr.push_back('X');
                        retStr.push_back('X');
                        break;
                    case 30:
                        retStr.push_back('X');
                        retStr.push_back('X');
                        retStr.push_back('X');
                        break;
                    case 40:
                        retStr.push_back('X');
                        retStr.push_back('L');          
                        break;
                    case 50:
                        retStr.push_back('L');
                        break;
                    case 60:
                        retStr.push_back('L');
                        retStr.push_back('X');
                        break;
                    case 70:
                        retStr.push_back('L');
                        retStr.push_back('X');
                        retStr.push_back('X');
                        break;
                    case 80:
                        retStr.push_back('L');
                        retStr.push_back('X');
                        retStr.push_back('X');
                        retStr.push_back('X');
                        break;
                    case 90:
                        retStr.push_back('X');
                        retStr.push_back('C');
                        break;
                      case 100:
                        retStr.push_back('C');
                        break;
                    case 200:
                        retStr.push_back('C');
                        retStr.push_back('C');
                        break;
                    case 300:
                        retStr.push_back('C');
                        retStr.push_back('C');
                        retStr.push_back('C');
                        break;
                    case 400:
                        retStr.push_back('C');
                        retStr.push_back('D');
                        break;
                    case 500:
                        retStr.push_back('D');
                        break;
                    case 600:
                        retStr.push_back('D');
                        retStr.push_back('C');
                        break;
                    case 700:
                        retStr.push_back('D');
                        retStr.push_back('C');
                        retStr.push_back('C');
                        break;
                    case 800:
                        retStr.push_back('D');
                        retStr.push_back('C');
                        retStr.push_back('C');
                        retStr.push_back('C');
                        break;
                    case 900:
                        retStr.push_back('C');
                        retStr.push_back('M');
                        break;
                       case 1000:
                             retStr.push_back('M');
                             break;
                        case 2000:
                            retStr.push_back('M');
                            retStr.push_back('M');
                            break;
                    case 3000:
                            retStr.push_back('M');
                            retStr.push_back('M');
                            retStr.push_back('M');
                           break;
                    }//end switch
                    tNextNum = getNextNum(num); 
                }//end while
                return retStr;
            }   
            //  获取下一个要编码的数字
            int getNextNum(int num)
            {   
                if (0==num)
                    return 0;
                if(num<10)
                    return num;
                int ret = 0;
                int carry = 1;
                num /=10;
                int tmp = num;
                while(num)
                {   
                    tmp = num;
                    carry *=10;
                    num /=10;
                }   
                return tmp*carry;
            }   
        };  
    ‵‵‵

    相关文章

      网友评论

        本文标题:LeetCode12: Integer to Roman

        本文链接:https://www.haomeiwen.com/subject/xookdftx.html