#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
int romanToInt(string s) {
int result = 0;
unordered_map<char, int> Roman;
Roman['I'] = 1;
Roman['V'] = 5;
Roman['X'] = 10;
Roman['L'] = 50;
Roman['C'] = 100;
Roman['D'] = 500;
Roman['M'] = 1000;
for (int i = 0; i < s.size(); ++i)
{
if (i == s.size() - 1 || Roman[s[i]] >= Roman[s[i + 1]]) result += Roman[s[i]];
else result -= Roman[s[i]];
}
return result;
}
};
int main()
{
string s;
Solution so;
while (cin >> s) cout << so.romanToInt(s) << endl;
return 0;
}
13.罗马数字转化为阿拉伯数字1-3999
最新推荐文章于 2019-06-05 15:01:19 发布