forked from SaifulJnU/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
romanToInteger.c
69 lines (68 loc) · 1.69 KB
/
romanToInteger.c
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
int romanToInt(char s[]) {
int c = s.length(),i=0,n=0;
char l='a',r='a';
while(i < c){
l = s[i];
if(i == c-1)
r = 'a';
else
r = s[i+1];
if(l == 'I'){
if(r == 'V'){
n += 4;
i += 2;
}
else if(r == 'X'){
n += 9;
i += 2;
}
else{
n++;
i++;
}
}
else if(l == 'V'){
n += 5;
i++;
}
else if(l == 'X'){
if(r == 'L'){
n += 40;
i += 2;
} else if(r == 'C'){
n += 90;
i += 2;
} else {
n += 10;
i++;
}
}
else if(l == 'L'){
n += 50;
i++;
}
else if(l == 'C'){
if(r == 'D'){
n += 400;
i += 2;
}
else if(r == 'M'){
n += 900;
i += 2;
}
else {
n += 100;
i++;
}
}
else if(l == 'D'){
n += 500;
i++;
}
else if(l == 'M'){
n += 1000;
i++;
}
}
return n;
}