Roman to Integer

问题:罗马数字变为整数

class Solution {

public:

    int romanToInt(string s) {

         char c[10][10][10]={{"0","I","II","III","IV","V","VI","VII","VIII","IX"},{"0","X","XX","XXX","XL","L","LX"

        ,"LXX","LXXX","XC"},{"0","C","CC","CCC","CD","D",

              "DC","DCC","DCCC","CM"},{"0","M","MM","MMM"}};

         int num=0,i;

         for(i=0;i<s.size();i++)

         {

             if(s[i]=='I') num+=1;

             if(s[i]=='V') 

             {

                if(i!=0 && s[i-1]=='I') num+=3;

                else num+=5;

             }

             if(s[i]=='X')

             {

                 if(i!=0 && s[i-1]=='I') num+=8;

                 else num+=10;

             }

             if(s[i]=='L')

             {

                 if(i!=0 && s[i-1]=='X') num+=30;

                 else num+=50;

             }

             if(s[i]=='C')

             {

                 if(i!=0 && s[i-1]=='X') num+=80;

                 else num+=100;

             }

             if(s[i]=='D')

             {

                 if(i!=0 && s[i-1]=='C') num+=300;

                 else num+=500;

             }

             if(s[i]=='M')  

             {

                 if(i!=0 && s[i-1]=='C') num+=800;

                 else num+=1000;

             }

         }

         return num;

    }

};

  

你可能感兴趣的:(Integer)