-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.java
More file actions
52 lines (47 loc) · 1.45 KB
/
Copy pathCode.java
File metadata and controls
52 lines (47 loc) · 1.45 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
package Homework;
public class Code {
public static void main(String[] args) {
class Roman {
int value(char r) {
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int romanToInt(String s) {
int total = 0;
for (int i = 0; i < s.length(); i++) {
int s1 = value(s.charAt(i));
if (i + 1 < s.length()) {
int s2 = value(s.charAt(i + 1));
if (s1 >= s2) {
total = total + s1;
} else {
total = total - s1;
}
} else {
total = total + s1;
}
}
return total;
}
// Driver code
public static void main(String args[]) {
Roman ob = new Roman();
String val = "MCMXCIV";
System.out.println(ob.romanToInt(val));
}
}
}
}