-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRoman_To_Int.java
More file actions
51 lines (51 loc) · 1.17 KB
/
Copy pathRoman_To_Int.java
File metadata and controls
51 lines (51 loc) · 1.17 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
import java.util.*;
class Roman_To_Int
{
public int value(char c)
{
switch(c)
{
case 'M':
return 1000;
case 'D':
return 500;
case 'C':
return 100;
case 'L':
return 50;
case 'X':
return 10;
case 'V':
return 5;
default:
return 1;
}
}
public int romanToInt(String s) {
int sum=0;
for(int i=0;i<s.length();i++)
{
if((i+1)<s.length())
{
if(value(s.charAt(i))>=value(s.charAt(i+1)))
{
sum+=value(s.charAt(i));
}
else
{
sum=sum+value(s.charAt(i+1))-value(s.charAt(i));
i++;
}
}
else if((i+1)==s.length())
sum+=value(s.charAt(i));
}
return sum;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
Roman_To_Int obj=new Roman_To_Int();
System.out.println(obj.romanToInt(s));
}
}