-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ03023_SoLaMa.java
34 lines (31 loc) · 1 KB
/
J03023_SoLaMa.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
import java.util.*;
import java.math.*;
import java.io.*;
public class J03023_SoLaMa {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
int n = s.length();
char[] c = s.toCharArray();
int sum = 0;
for (int i = n - 1; i >= 0; i--) {
sum += slm(c[i]);
int j = i - 1, flag = 0;
while (j >= 0 && slm(c[i]) > slm(c[j])) {
sum -= slm(c[j]);
j--;
flag = 1;
}
if (flag > 0) {
i = j + 1;
}
}
System.out.println(sum);
}
}
public static int slm(char c) {
return ((c == 'I') ? 1 : ((c == 'V') ? 5 : ((c == 'X') ? 10 : ((c == 'L') ? 50 : ((c == 'C') ? 100 : ((c == 'D') ? 500 : (1000)))))));
}
}