-
Notifications
You must be signed in to change notification settings - Fork 0
/
RomanToInteger.c
84 lines (82 loc) · 1.83 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
int romanToInt(char * s) {
int past = 0;
int sum = 0;
for (int i = 0; i < 16; i++) {
if (s[i] == 'M') {
if (past == 100) {
sum = sum + 1000 - 200;
past = 1000;
}
else {
sum = sum + 1000;
past = 1000;
}
}
else if (s[i] == 'D') {
if (past == 100) {
sum = sum + 500 - 200;
past = 500;
}
else {
sum = sum + 500;
past = 500;
}
}
else if (s[i] == 'C') {
if (past == 10) {
sum = sum + 100 - 20;
past = 100;
}
else {
sum = sum + 100;
past = 100;
}
}
else if (s[i] == 'L') {
if (past == 10) {
sum = sum + 50 - 20;
past = 50;
}
else {
sum = sum + 50;
past = 50;
}
}
else if (s[i] == 'X') {
if (past == 1) {
sum = sum + 10 - 2;
past = 10;
}
else {
sum = sum + 10;
past = 10;
}
}
else if (s[i] == 'V') {
if (past == 1) {
sum = sum + 5 - 2;
past = 5;
}
else {
sum = sum + 5;
past = 5;
}
}
else if (s[i] == 'I') {
++sum;
past = 1;
}
else {
return sum;
}
}
return sum;
}
int main() {
char s[] = "MCMXCIV";
printf("%d", romanToInt(s));
}