forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_simulation_1.cpp
42 lines (37 loc) · 874 Bytes
/
AC_simulation_1.cpp
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
37
38
39
40
41
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_1.cpp
* Create Date: 2015-01-08 14:47:56
* Descripton: simulation
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
int val[255];
void init() {
val['I'] = 1; val['V'] = 5; val['X'] = 10; val['L'] = 50;
val['C'] = 100; val['D'] = 500; val['M'] = 1000;
}
public:
int romanToInt(string s) {
init();
int ret = 0;
for (int i = 0; i < s.size(); i++) {
if (i > 0 && val[s[i]] > val[s[i - 1]]) {
ret += val[s[i]] - 2 * val[s[i - 1]];
} else {
ret += val[s[i]];
}
}
return ret;
}
};
int main() {
string str;
Solution s;
while (cin >> str)
cout << s.romanToInt(str) << endl;
return 0;
}