Convert roman numerals to Arabic numerals

Source: Internet
Author: User

 1 /* I:1 ; V-5;X-10;L-50;C-100;D-500;M-1000*/ 2 class Solution{ 3     vector<string> tokens; 4     vector<char> token_value; 5     public: 6     /*divide the string into some tokens,every token including same char*/ 7     void token(string s) 8     { 9         string::iterator i=s.begin();10         string::iterator j=s.begin();11 12         while(i!=s.end())13         {14             char cur=*i;15             token_value.push_back(cur);16             while(*i == cur){17                 i++;18             }19 20             string temp(j,i);21             tokens.push_back(temp);22             j=i;23         }24     }25     /*比较相邻字母的大小*/26     bool lower(char a,char b)27     {28         if((a==‘I‘ && b!=‘I‘)||(a==‘V‘ && b!=‘I‘&&b!=‘V‘) || (a==‘X‘&&b!=‘I‘&&b!=‘V‘&&b!=‘X‘)|| 29                 (a==‘L‘&&b!=‘I‘&&b!=‘V‘&&b!=‘X‘&&b!=‘L‘)||(a==‘C‘ && (b==‘D‘ || b==‘M‘)) || (a==‘D‘&&b==‘M‘))30             return true;31         else return false;32     }33     int romanToInt(string s){34         token(s);35         int result=0;36         vector<int> res;37         /*将上述tokens转为一个int数组*/38         for(vector<string>::iterator i=tokens.begin();i!=tokens.end();i++)39         {40             string temp=*i;41             int temp_value=0;42             char cur=temp[0];43             int len=temp.size();44             for(int i=0;i<len;i++)45             {46                 switch(cur)47                 {48                     case ‘I‘:temp_value++;break;49                     case ‘V‘:temp_value+=5;break;50                     case ‘X‘:temp_value+=10;break;51                     case ‘L‘:temp_value+=50;break;52                     case ‘C‘:temp_value+=100;break;53                     case ‘D‘:temp_value+=500;break;54                     case ‘M‘:temp_value+=1000;break;55                     default:break;56                 }57 58             }59             res.push_back(temp_value);60 61         }62         result=res[res.size()-1];63         for(int i=token_value.size()-2;i>=0;i--)64         {65             if(lower(token_value[i],token_value[i+1])){66                 result-=res[i];67             }68             else{69                 result+=res[i];70             }71             cout<<result<<endl;72         }73         return result;74     }75 };
1. 首先去维基百科查阅罗马数字的规范和表示方法;
2. 由于做词法分析的缘故,对字符串的问题总是习惯分解成token来做,首先我将罗马数字按照其类别分成很多组,每一组内的元素都是相同的,然后将每组的值计算出来;
3. 现在得到了2个新的数组,一个是由罗马数字类别组成的数组(可看成原字符串去掉重复之后的结果),另一个是上一个数组的结果数组;
4. 最后我们根据第一个数组的char 的大小关系来对结果数组进行求值,从右向左求值,左边比右边大,就加上左边的,否则减去左边的值。

Convert roman numerals to Arabic numerals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.