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 }
My Github Blog: mdgsf.github.io

浙公网安备 33010602011771号