roman to interger

将罗马数字转化为阿拉伯数字。

 

 1 int romanToInt(string s) 
 2 {
 3     int iResult = 0;
 4     map<char, int> mRoman;
 5     mRoman['I'] = 1;
 6     mRoman['V'] = 5;
 7     mRoman['X'] = 10;
 8     mRoman['L'] = 50;
 9     mRoman['C'] = 100;
10     mRoman['D'] = 500;
11     mRoman['M'] = 1000;
12     for (int i=s.length()-1; i>=0; i--)
13     {
14         if(i == (s.length()-1))
15         {
16             iResult = mRoman[s[i]];
17             continue;
18         }
19         if(mRoman[s[i]] >= mRoman[s[i+1]])
20         {
21             iResult += mRoman[s[i]];
22         }
23         else
24         {
25             iResult -= mRoman[s[i]];
26         }
27     }
28     return iResult;
29 }

 

posted @ 2014-12-02 18:38  MDGSF  阅读(128)  评论(0)    收藏  举报