- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem13.java
More file actions
Latest commit
33 lines (31 loc) · 914 Bytes
/
Copy pathProblem13.java
File metadata and controls
33 lines (31 loc) · 914 Bytes
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
classProblem13 {
publicstaticvoidmain(String[] args) {
Strings = "MCMXCIV";
intlen = newProblem13().romanToInt(s);
System.out.println(len);
}
publicintromanToInt(Strings) {
intsum = 0;
intpre = -1;
intcur = 0;
for (inti = 0; i < s.length(); i++) {
charc = s.charAt(i);
switch (c) {
case'I': cur = 1; break;
case'V': cur = 5; break;
case'X': cur = 10; break;
case'L': cur = 50; break;
case'C': cur = 100; break;
case'D': cur = 500; break;
case'M': cur = 1000; break;
}
if (pre > 0 && pre < cur) {
sum = sum - 2 * pre + cur;
} else {
sum += cur;
}
pre = cur;
}
returnsum;
}
}