public class a013 {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
while (sc.hasNext()) {
String p = sc.nextLine();
if (p.equals("#"))
break;
int l = p.length();
String[] q = new String[2];
for (int i = 0; i < p.length(); i++) {
String e = p.substring(i, i + 1);
if (e.equals(" ")) {
q[0] = p.substring(0, i);
q[1] = p.substring(i + 1, l);
break;
}
}
int[][] r = new int[2][100];
for (int i = 0; i < q[0].length(); i++) {
String e = q[0].substring(i, i + 1);
switch (e) {
case "M":
r[0][i] = 1000;
break;
case "D":
r[0][i] = 500;
break;
case "C":
r[0][i] = 100;
break;
case "L":
r[0][i] = 50;
break;
case "X":
r[0][i] = 10;
break;
case "V":
r[0][i] = 5;
break;
case "I":
r[0][i] = 1;
break;
}
}
for (int i = 0; i < q[1].length(); i++) {
String e = q[1].substring(i, i + 1);
switch (e) {
case "M":
r[1][i] = 1000;
break;
case "D":
r[1][i] = 500;
break;
case "C":
r[1][i] = 100;
break;
case "L":
r[1][i] = 50;
break;
case "X":
r[1][i] = 10;
break;
case "V":
r[1][i] = 5;
break;
case "I":
r[1][i] = 1;
break;
}
}
int total1 = 0;
int total2 = 0;
for (int i = 0; i < q[0].length(); i++) {
if (i == 0) {
if (r[0][i] - r[0][i + 1] < 0) {
total1 += r[0][i + 1] - r[0][i];
} else {
total1 += r[0][i] + r[0][i + 1];
}
} else {
if (r[0][i] - r[0][i + 1] < 0) {
total1 -= r[0][i];
} else {
total1 += r[0][i + 1];
}
}
}
for (int i = 0; i < q[1].length(); i++) {
if (i == 0) {
if (r[1][i] - r[1][i + 1] < 0) {
total2 += r[1][i + 1] - r[1][i];
} else {
total2 += r[1][i] + r[1][i + 1];
}
} else {
if (r[1][i] - r[1][i + 1] < 0) {
total2 -= r[1][i];
} else {
total2 += r[1][i + 1];
}
}
}
int d = 0;
if (total1 - total2 < 0) {
d = total2 - total1;
} else {
d = total1 - total2;
}
String ans = "";
if (d == 0) {
System.out.println("ZERO");
}
if (d >= 1000) {
int i = d / 1000;
for (int j = 0; j < i; j++) {
ans += "M";
}
d -= 1000 * (d / 1000);
}
if (d / 100 == 4) {
ans += "CD";
d -= 400;
} else if (d / 100 == 9) {
ans += "CM";
d -= 900;
} else {
if (d >= 500) {
ans += "D";
d -= 500;
}
if (d >= 100) {
int i = d / 100;
for (int j = 0; j < i; j++) {
ans += "C";
}
d -= 100 * (d / 100);
}
}
if (d / 10 == 4) {
ans += "XL";
d -= 40;
} else if (d / 10 == 9) { //9x
ans += "XC";
d -= 90;
} else {
if (d >= 50) {
ans += "L";
d -= 50;
}
if (d >= 10) {
int i = d / 10;
for (int j = 0; j < i; j++) {
ans += "X";
}
d -= 10 * (d / 10);
}
}
if (d == 4) {
ans += "IV";
}else if (d == 9) {
ans += "IX";
} else {
if (d >= 5) {
ans += "V";
d -= 5;
}
for (int j = 0; j < d; j++) {
ans += "I";
}
}
System.out.println(ans);
}
}
}
你的答案是1007
正確答案是1997
錯在......
我也不知道XD