-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInteger.java
More file actions
89 lines (83 loc) · 2.39 KB
/
Copy pathRomanToInteger.java
File metadata and controls
89 lines (83 loc) · 2.39 KB
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
85
86
87
88
89
package EasyCode;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class RomanToInteger {
public static void main(String[] args) {
// InputStream input = new InputStream() {
// @Override
// public int read() throws IOException {
// return 0;
// }
// };
// String s = null;
// Scanner sc = new Scanner(input);
// while(sc.hasNext()){
// s = sc.nextLine();
// System.out.println(s);
// }
String s = "MDLXX";
char [] chars = s.toCharArray();
for(char c : chars)
System.out.print(c);
int result = 0;
for(int i = 0 ; i < chars.length ; i++){
switch (chars[i]){
case 'M':
result+=1000;
break;
case 'D':
result+=500;
break;
case 'C':
if(chars[i+1] == 'D' && i+1 < chars.length){
result+=400;
i++;
}
else if(chars[i+1] == 'M' && i+1 < chars.length){
result+=900;
i++;
}
else
result+=100;
break;
case 'L':
result+=50;
break;
case 'X':
if(chars[i+1] == 'L' && i+1 < chars.length){
result+=40;
i++;
}
else if(chars[i+1] == 'C' && i+1 < chars.length){
result+=90;
i++;
}
else
result+=10;
break;
case 'V':
result+=5;
break;
case 'I':
if(i == chars.length - 1){
result+=1;
break;
}
if(chars[i+1] == 'V'){
result+=4;
i++;
}
else if(chars[i+1] == 'X'){
result+=9;
i++;
}
else
result+=1;
break;
default:
break;
}
}
}
}