-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet13.cpp
More file actions
64 lines (60 loc) · 1.17 KB
/
Copy pathleet13.cpp
File metadata and controls
64 lines (60 loc) · 1.17 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std ;
//Roman to Integer
class Solution {
int romans[26] ;
public:
Solution(){
init() ;
}
int romanToInt(string s) {
if (s.length() == 0)
{
return 0 ;
}
char a, b;
a = s[0] ;
int va, vb ;
va = romans[s[0]-'A'] ;
for(int i=1 ;i<s.length() ;i++){
//cout << "Now : " << s[i] << ", " << va << endl ;
if(s[i] == a){
va += value(s[i]);
}else{
if(value(s[i])>value(a)){
va = va-2*value(a)+value(s[i]) ;
}else{
va = va+value(s[i]);
}
}
a = s[i] ;
}
// cout << va << endl ;
return va ;
}
private:
void init(){
for(int i=0 ;i<26 ;i++){
romans[i] = 0 ;
}
romans['I'-'A'] = 1 ;
romans['V'-'A'] = 5 ;
romans['X'-'A'] = 10 ;
romans['C'-'A'] = 100 ;
romans['L'-'A'] = 50 ;
romans['D'-'A'] = 500 ;
romans['M'-'A'] = 1000 ;
}
int value(char c){
return romans[c-'A'] ;
}
};
int main(){
string s = "XIII" ;
Solution sl ;
while(1){
cin >> s ;
cout << sl.romanToInt(s) << endl ;
}
//cout << sl.romanToInt(s) << endl ;
}