-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromToInt.cpp
More file actions
65 lines (59 loc) · 1.49 KB
/
Copy pathromToInt.cpp
File metadata and controls
65 lines (59 loc) · 1.49 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
65
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int romanToInt(string s) {
int total = 0;
for(int i = 0;i<s.length();i++){
if(s[i] == 'I'){
if(s[i+1] == 'V'){
total += 4;
i++;
}else if(s[i+1] == 'X'){
total += 9;
i++;
}else{
total += 1;
}
}else if(s[i] == 'V'){
total += 5;
}else if(s[i] == 'X'){
if(s[i+1] == 'L'){
total += 40;
i++;
}else if(s[i+1] == 'C'){
total += 90;
i++;
}else{
total += 10;
}
}else if(s[i] == 'L'){
total += 50;
}else if(s[i] == 'C'){
if(s[i+1] == 'D'){
total += 400;
i++;
}else if(s[i+1] == 'M'){
total += 900;
i++;
}else{
total += 100;
}
}else if(s[i] == 'D'){
total += 500;
}else if(s[i] == 'M'){
total += 1000;
}
}
return total;
}
};
int main(){
Solution s1;
string s;
cout<<"Enter String here: ";
cin>>s;
cout<<s1.romanToInt(s);
return 0;
}