-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromanToInt.java
54 lines (54 loc) · 1.63 KB
/
romanToInt.java
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
public class romanToInt {
public static int toInt(String s){
int x = 0;
int i = 0;
int len = s.length();
while (i < len){
if (i != len - 1 && s.charAt(i) == 'I' && s.charAt(i + 1) == 'V') {
x += 4;
i++;
}
else if (i != len - 1 && s.charAt(i) == 'I' && s.charAt(i + 1) == 'X') {
x += 9;
i++;
}
else if (s.charAt(i) == 'I') {
x += 1;
}
else if (s.charAt(i) == 'V')
x += 5;
else if (i != len - 1 && s.charAt(i) == 'X' && s.charAt(i + 1) == 'L') {
x += 40;
i++;
}
else if (i != len - 1 && s.charAt(i) == 'X' && s.charAt(i + 1) == 'C'){
x += 90;
i++;
}
else if (s.charAt(i) == 'X')
x += 10;
else if (s.charAt(i) == 'L')
x += 50;
else if (i != len - 1 && s.charAt(i) == 'C' && s.charAt(i + 1) == 'D'){
x += 400;
i++;
}
else if (i != len - 1 && s.charAt(i) == 'C' && s.charAt(i + 1) == 'M'){
x += 900;
i++;
}
else if (s.charAt(i) == 'C')
x += 100;
else if (s.charAt(i) == 'D')
x += 500;
else if (s.charAt(i) == 'M')
x += 1000;
i++;
}
return (x);
}
public static void main(String[] args){
String s = "MCMXCIV";
System.out.println(toInt(s));
}
}