您的位置:首页 > 其它

13.罗马数字转化为阿拉伯数字1-3999

2014-10-06 18:33 701 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: