-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAromaticNumbers.java
More file actions
47 lines (43 loc) · 1.33 KB
/
AromaticNumbers.java
File metadata and controls
47 lines (43 loc) · 1.33 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
import java.io.*;
import java.util.*;
public class AromaticNumbers
{
public static void main(String[] args) throws IOException
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
String[] input = b.readLine().split("");
int sum = 0;
if(input.length > 2)
{
for(int i = 0;i<input.length-3;i+=2)
{
int nextR = value(input[i+3]);
int thisR = value(input[i+1]);
if(nextR>thisR)
{
sum -= Integer.parseInt(input[i])*value(input[i+1]);
}
else
{
sum += Integer.parseInt(input[i])*value(input[i+1]);
}
}
sum += Integer.parseInt(input[input.length-2])*value(input[input.length-1]);
System.out.println(sum);
}
else
{
System.out.println(Integer.parseInt(input[0])*value(input[1]));
}
}
public static int value(String s)
{
if(s.equals("I")) return 1;
else if(s.equals("V")) return 5;
else if(s.equals("X")) return 10;
else if(s.equals("L")) return 50;
else if(s.equals("C")) return 100;
else if(s.equals("D")) return 500;
else return 1000;
}
}