-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.cpp
More file actions
36 lines (30 loc) · 685 Bytes
/
Copy path13.cpp
File metadata and controls
36 lines (30 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Solution
{
public:
int romanToInt(string s)
{
map<char, int> map = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
int total = 0;
int previous = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
int value = map[s[i]];
if (value < previous)
total -= value;
else
total += value;
previous = map[s[i]];
}
return total;
}
};
int main(){
string s;
cin >> s;
Solution sol;
cout << sol.romanToInt(s) << endl;
};