-
Notifications
You must be signed in to change notification settings - Fork 9
/
LC_PROBLEM13.cpp
72 lines (66 loc) · 1.53 KB
/
LC_PROBLEM13.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
class Solution {
public:
int romanToInt(string s) {
int I=1,V=5,X=10,L=50,C=100,D=500,M=1000;
int integer,len=0,sum=0;
for(int i=0;s[i]!='\0';i++)
len++;
if(len>=1 && len<=15)
{
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='I')
{
if(s[i+1]=='V')
{integer=V-1;
i++;
sum=sum+integer;}
else if(s[i+1]=='X')
{integer=X-1;
i++;
sum=sum+integer;}
else
sum=sum+I;
}
else if(s[i]=='X')
{
if(s[i+1]=='L')
{integer=L-10;
i++;
sum=sum+integer;}
else if(s[i+1]=='C')
{integer=C-10;
i++;
sum=sum+integer;}
else
sum=sum+X;
}
else if(s[i]=='C')
{
if(s[i+1]=='D')
{integer=D-100;
i++;
sum=sum+integer;}
else if(s[i+1]=='M')
{integer=M-100;
i++;
sum=sum+integer;}
else
sum=sum+C;
}
else
{
if(s[i]=='V')
sum=sum+V;
else if(s[i]=='L')
sum=sum+L;
else if(s[i]=='D')
sum=sum+D;
else if(s[i]=='M')
sum=sum+M;
}
}
}
return sum;
}
};