void printf_a(int i, char ok) {
for (int j = 0; j < i; j++) {
printf("%c", ok);
}
}
void changeagain(int str, int id) {
char ok;
if (id == 0) {
int i = str;
ok = 'I';
if (str < 9 && str > 4) {
printf("V");
i = str - 5;
printf_a(i, ok);
}
else if (str < 4 && str >= 0) printf_a(i, ok);
else if (str == 9) printf("IX");
else if (str == 4) printf("IV");
}
else if (id == 1) {
int i = str;
ok = 'X';
if (9 > str && str > 4) {
printf("L");
i = str - 5;
printf_a(i, ok);
}
else if (str < 4 && str >= 0)printf_a(i, ok);
else if (str == 9) printf("XC");
else if (str == 4) printf("XL");
}
else if (id == 2) {
ok = 'C';
int i = str;
if (str < 9 && str > 4) {
printf("D");
i = str - 5;
printf_a(i, ok);
}
else if (str >= 0 && str < 4)printf_a(str, ok);
else if (str == 9) printf("CM");
else if (str == 4) printf("CD");
}
else if (id == 3) {
ok = 'M';
int i = str;
printf_a(i, ok);
}
}
int change_to_integer(char* str) {
int length_a = strlen(str);
int carry = 0;
for (int i = length_a; i >= 0; i--) {
if (str[i] == 'I') {
carry = 1 + carry;
}
else if(str[i] == 'V') {
if (str[i - 1] == 'I') {
carry = 4 + carry;
i--;
}
else carry = 5 + carry;
}
else if (str[i] == 'X') {
if (str[i - 1] == 'I') {
carry = carry + 9;
i--;
}
else carry = 10 + carry;
}
else if (str[i] == 'L') {
if (str[i - 1] == 'X') {
carry = carry + 40;
i--;
}
else carry = 50 + carry;
}
else if (str[i] == 'C') {
if (str[i - 1] == 'X') {
carry = carry + 90;
i--;
}
else carry = 100 + carry;
}
else if (str[i] == 'D') {
if (str[i - 1] == 'C') {
carry = carry + 400;
i--;
}
else carry = 500 + carry;
}
else if (str[i] == 'M') {
carry = 1000 + carry;
}
}
return (carry);
}
int main() {
char str1[100];
char str2[100];
while (1) {
int stop = 0;
scanf("%s", &str1);
for (int i = 0; str1[i] != '\0'; i++) {
if (str1[i] == '#') stop++;
}
if (stop != 0) break;
scanf("%s", &str2);
for (int i = 0; str2[i] != '\0'; i++) {
if (str2[i] == '#') stop++;
}
if (stop != 0) break;
int answer = abs(change_to_integer(str1) - change_to_integer(str2));
if (answer == 0) printf("ZERO");
else {
int temp1 = answer - (answer / 1000) * 1000;
int temp2 = answer - (answer / 100) * 100;
changeagain(answer / 1000, 3);
changeagain(temp1 / 100, 2);
changeagain(temp2 / 10, 1);
changeagain(answer % 10, 0);
}
printf("\n");
}
return 0;
}
void printf_a(int i, char ok) {
for (int j = 0; j < i; j++) {
printf("%c", ok);
}
}
void changeagain(int str, int id) {
char ok;
if (id == 0) {
int i = str;
ok = 'I';
if (str < 9 && str > 4) {
printf("V");
i = str - 5;
printf_a(i, ok);
}
else if (str < 4 && str >= 0) printf_a(i, ok);
else if (str == 9) printf("IX");
else if (str == 4) printf("IV");
}
else if (id == 1) {
int i = str;
ok = 'X';
if (9 > str && str > 4) {
printf("L");
i = str - 5;
printf_a(i, ok);
}
else if (str < 4 && str >= 0)printf_a(i, ok);
else if (str == 9) printf("XC");
else if (str == 4) printf("XL");
}
else if (id == 2) {
ok = 'C';
int i = str;
if (str < 9 && str > 4) {
printf("D");
i = str - 5;
printf_a(i, ok);
}
else if (str >= 0 && str < 4)printf_a(str, ok);
else if (str == 9) printf("CM");
else if (str == 4) printf("CD");
}
else if (id == 3) {
ok = 'M';
int i = str;
printf_a(i, ok);
}
}int change_to_integer(char* str) {
int length_a = strlen(str);
int carry = 0;
for (int i = length_a; i >= 0; i--) {
if (str[i] == 'I') {
carry = 1 + carry;
}
else if(str[i] == 'V') {
if (str[i - 1] == 'I') {
carry = 4 + carry;
i--;
}
else carry = 5 + carry;
}
else if (str[i] == 'X') {
if (str[i - 1] == 'I') {
carry = carry + 9;
i--;
}
else carry = 10 + carry;
}
else if (str[i] == 'L') {
if (str[i - 1] == 'X') {
carry = carry + 40;
i--;
}
else carry = 50 + carry;
}
else if (str[i] == 'C') {
if (str[i - 1] == 'X') {
carry = carry + 90;
i--;
}
else carry = 100 + carry;
}
else if (str[i] == 'D') {
if (str[i - 1] == 'C') {
carry = carry + 400;
i--;
}
else carry = 500 + carry;
}
else if (str[i] == 'M') {
carry = 1000 + carry;
}
}
return (carry);
}
int main() {
char str1[100];
char str2[100];
while (1) {
int stop = 0;
scanf("%s", &str1);
for (int i = 0; str1[i] != '\0'; i++) {
if (str1[i] == '#') stop++;
}
if (stop != 0) break;
scanf("%s", &str2);
for (int i = 0; str2[i] != '\0'; i++) {
if (str2[i] == '#') stop++;
}
if (stop != 0) break;
int answer = abs(change_to_integer(str1) - change_to_integer(str2));
if (answer == 0) printf("ZERO");
else {
int temp1 = answer - (answer / 1000) * 1000;
int temp2 = answer - (answer / 100) * 100;
changeagain(answer / 1000, 3);
changeagain(temp1 / 100, 2);
changeagain(temp2 / 10, 1);
changeagain(answer % 10, 0);
}
printf("\n");
}
return 0;
}
CM是900