题目:Given a roman numeral, convert it to aninteger.
Input is guaranteed to be within the rangefrom 1 to 3999.
代码如下:
//罗马数字转为int型整数
int RomanToint(string &s ) {
unordered_map<char, int> T = { { 'I', 1 },
{ 'V', 5 },{ 'X', 10 },{ 'L', 50 },{ 'C', 100 },
{ 'D', 500 },{ 'M', 1000 } };
int sum = T[s.back()];
for (int i = s.length() - 2; i >= 0; --i)
if (T[s[i]] < T[s[i + 1]])
sum -= T[s[i]];
else
sum += T[s[i]];
return sum;
}