# 罗马数字转整数

罗马数字包含以下七种字符: I, V, X, LCD 和 M

字符          数值
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

给你一个整数,将其转为罗马数字。

 

示例 1:

输入: num = 3

输出:
"III"

示例 2:

输入: num = 4

输出:
"IV"

示例 3:

输入: num = 9

输出:
"IX"

示例 4:

输入: num = 58

输出:
"LVIII"
解释:
L = 50, V = 5, III = 3.

示例 5:

输入: num = 1994

输出:
"MCMXCIV"
解释:
M = 1000, CM = 900, XC = 90, IV = 4.

 

提示:

以下错误的选项是?

## aop ### before ```cpp #include using namespace std; ``` ### after ```cpp int main() { Solution sol; cout << sol.romanToInt("LVIII") << endl; return 0; } ``` ## 答案 ```cpp class Solution { public: int getTwoNum(char num1, char num2) { if (num1 == 'I' && (num2 == 'V' || num2 == 'X')) { if (num2 == 'V') { return 4; } else { return 9; } } if (num1 == 'X' && (num2 == 'L' || num2 == 'C')) { if (num2 == 'L') { return 40; } else { return 90; } } if (num1 == 'C' && (num2 == 'D' || num2 == 'M')) { if (num2 == 'D') { return 400; } else { return 900; } } else { return -1; } } int romanToInt(string s) { int index = 0; int sum = 0; while (index < s.length()) { char romeNum1 = s[index]; if (index == s.length()) { sum = getNum(romeNum1); } else { char romeNum2 = s[index + 1]; int twosum = getTwoNum(romeNum1, romeNum2); if (twosum > 0) { sum += twosum; index++; } else { sum = getNum(romeNum1); } } index++; } return sum; } private: int getNum(char roma) { switch (roma) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: return 0; } } }; ``` ## 选项 ### A ```cpp class Solution { public: int romanToInt(string s) { int result = 0; int n = s.length(); for (int i = 0; i < n; i++) { switch (s[i]) { case 'I': if (i < n - 1 && s[i + 1] == 'V') { result += 4; i++; } else if (i < n - 1 && s[i + 1] == 'X') { result += 9; i++; } else result++; break; case 'V': result += 5; break; case 'X': if (i < n - 1 && s[i + 1] == 'L') { result += 40; i++; } else if (i < n - 1 && s[i + 1] == 'C') { result += 90; i++; } else result += 10; break; case 'L': result += 50; break; case 'C': if (i < n - 1 && s[i + 1] == 'D') { result += 400; i++; } else if (i < n - 1 && s[i + 1] == 'M') { result += 900; i++; } else result += 100; break; case 'D': result += 500; break; case 'M': result += 1000; } } return result; } }; ``` ### B ```cpp class Solution { public: int romanToInt(string s) { int result = 0; map luomab; luomab.insert(map::value_type('I', 1)); luomab.insert(map::value_type('V', 5)); luomab.insert(map::value_type('X', 10)); luomab.insert(map::value_type('L', 50)); luomab.insert(map::value_type('C', 100)); luomab.insert(map::value_type('D', 500)); luomab.insert(map::value_type('M', 1000)); for (int i = 0; i < s.length(); i++) { if (luomab[s[i]] >= luomab[s[i + 1]]) result += luomab[s[i]]; else { result += (luomab[s[i + 1]] - luomab[s[i]]); i++; } } return result; } }; ``` ### C ```cpp class Solution { public: int romanToInt(string s) { int sum = 0; for (int i = 1; i <= s.size(); i++) { if (s[i - 1] == 'I' && s[i] == 'V') { sum += 4; i++; } else if (s[i - 1] == 'I' && s[i] == 'X') { sum += 9; i++; } else if (s[i - 1] == 'X' && s[i] == 'L') { sum += 40; i++; } else if (s[i - 1] == 'X' && s[i] == 'C') { sum += 90; i++; } else if (s[i - 1] == 'C' && s[i] == 'D') { sum += 400; i++; } else if (s[i - 1] == 'C' && s[i] == 'M') { sum += 900; i++; } else if (s[i - 1] == 'I') { sum += 1; } else if (s[i - 1] == 'V') { sum += 5; } else if (s[i - 1] == 'X') { sum += 10; } else if (s[i - 1] == 'L') { sum += 50; } else if (s[i - 1] == 'C') { sum += 100; } else if (s[i - 1] == 'D') { sum += 500; } else if (s[i - 1] == 'M') { sum += 1000; } } return sum; } }; ```