-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathRoman_Number_to_Integer.java
More file actions
49 lines (46 loc) · 1.09 KB
/
Roman_Number_to_Integer.java
File metadata and controls
49 lines (46 loc) · 1.09 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
import java.util.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
String str = sc.next();
System.out.println(romanToInt(str));
}
}
public static 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;
}
public static int romanToInt(String s) {
int total = 0;
int len = s.length();
for(int i = 0; i<len; i++){
int v1 = value( s.charAt(i));
if( i + 1 < len ){
int v2 = value( s.charAt(i+1) );
if( v1 >= v2 ){
total += v1;
}else{
total -= v1;
}
}else{
total += v1;
}
}
return total;
}
}