-
Notifications
You must be signed in to change notification settings - Fork 119
/
13.c
62 lines (60 loc) · 1.3 KB
/
13.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
#include <stdio.h>
#include <string.h>
int romanToInt(char *s) {
int len = strlen(s);
int ans = 0;
int i = 0;
while (i < len) {
switch (s[i]) {
case 'M':
ans += 1000;
break;
case 'D':
ans += 500;
break;
case 'C':
if (s[i + 1] == 'D' || s[i + 1] == 'M'){
ans -= 100;
}
else {
ans += 100;
}
break;
case 'L':
ans += 50;
break;
case 'X':
if (s[i + 1] == 'L' || s[i + 1] == 'C'){
ans -= 10;
}
else {
ans += 10;
}
break;
case 'V':
ans += 5;
break;
case 'I':
if (s[i + 1] == 'V' || s[i + 1] == 'X'){
ans -= 1;
}
else {
ans += 1;
}
break;
default:
break;
}
i++;
}
return ans;
}
int main() {
char s1[] = "MMXIV";
char s2[] = "MMXV";
/* should be 2014 */
printf("%d\n", romanToInt(s1));
/* should be 2015 */
printf("%d\n", romanToInt(s2));
return 0;
}