forked from suman-shah/c-program-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roman_To_Integer.cpp
76 lines (71 loc) · 1.61 KB
/
Roman_To_Integer.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int romanToInt(string s) {
int r =0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='V')
r += 5;
else if(s[i]=='L')
r += 50;
else if(s[i]=='D')
r += 500;
else if(s[i]=='M')
r += 1000;
else if(s[i]=='C')
{
if(s[i+1]=='D')
{
r += 400;
i++;
}
else if(s[i+1]=='M')
{
r += 900;
i++;
}
else
r += 100;
}
else if(s[i]=='X')
{
if(s[i+1]=='L')
{
r +=40;
i++;
}
else if(s[i+1]=='C')
{
r += 90;
i++;
}
else
r +=10 ;
}
else if(s[i]=='I')
{
if(s[i+1]=='V')
{
r += 4;
i++;
}
else if(s[i+1]=='X')
{
r += 9;
i++;
}
else
r += 1;
}
}
return r;
}
};
int main(){
Solution s ;
int result= s.romanToInt("III");
cout<<result ;
return 0;
}