-
Notifications
You must be signed in to change notification settings - Fork 0
/
romantoint.java
90 lines (75 loc) · 1.69 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
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
90
import java.util.*;
public class RomanToNumber
{
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 void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
String str=str1.toUpperCase();
int res = 0;
/* for (int i=str.length()-1; i>=0; i--)
{
int s2=value(str.charAt(i));
if( i > 0 )
{
int s1=value(str.charAt(i-1));
if( s1 < s2 )
{
res = res + s2 ;
}
else
{
res = res + s2 - s1 ;
i--;
}
}
else
{
res = res + s2 ; i--;
}
}
System.out.println("result :"+res);*/
res=0;
for(int i=0;i<str.length();i++)
{
int s1=value(str.charAt(i));
if(i < str.length()-1)
{
int s2=value(str.charAt(i+1));
if(s1 >= s2)
{
res = res + s1;
}
else
{
res = res + s2 - s1 ;
i++;
}
}
else
{
res = res + s1 ;
i++;
}
}
System.out.println("result :"+res);
}
}