-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion13.java
More file actions
115 lines (106 loc) · 1.67 KB
/
Copy pathQuestion13.java
File metadata and controls
115 lines (106 loc) · 1.67 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 13
*/
/*
TestCases-
*/
public class Question13
{
public static int romanToInt(String s)
{
int i;
char ch,ch2;
long sum = 0;
for(i=0;i<s.length();i++)
{
ch = s.charAt(i);
//System.out.println(ch);
if(i+1<s.length() && (ch == 'I' || ch == 'X' || ch == 'C'))
{
ch2 = s.charAt(i+1);
if(ch == 'I' && ch2 == 'X')
{
sum+=9;
i++;
}
else if(ch=='I' && ch2 == 'V')
{
sum+=4;
i++;
}
else if(ch=='X' && ch2 == 'L')
{
sum+=40;
i++;
}
else if(ch == 'X' && ch2 == 'C')
{
sum+=90;
i++;
}
else if(ch =='C' && ch2 == 'M')
{
sum+=900;
i++;
}
else if(ch=='C' && ch2 == 'D')
{
sum+=400;
i++;
}
else
{
switch(ch)
{
case 'I':
sum+=1;
break;
case 'C':
sum+=100;
break;
case 'X':
sum+=10;
break;
}
}
}
else
{
switch(ch)
{
case 'I':
sum+=1;
break;
case 'V':
sum+=5;
break;
case 'X':
sum+=10;
break;
case 'L':
sum+=50;
break;
case 'C':
sum+=100;
break;
case 'D':
sum+=500;
break;
case 'M':
sum+=1000;
break;
}
}
//System.out.println(sum);
//System.out.println(i);
}
return (int)sum;
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
System.out.println(romanToInt("DCXXII"));
}
}