#include <stdio.h>
int priority(char);
int numcheck(char, char);
void print(int);
main(){
char num1[2];
char num2[2];
while (scanf("%s%s", num1, num2) != '#'){
int sub1, sub2;//兩個數字分別的總和
sub1 = numcheck(num1[0], num1[1]);
sub2 = numcheck(num2[0], num2[1]);
int total;
if (sub1 >= sub2) total = sub1 - sub2;
else total = -sub1 + sub2;
if (total == 0) printf("ZERO");
else {
int allnum[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; //MDCLXVI
char allchar[13][2] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int i;
for (i = 0; i <= 12; i++){
while (total > allnum[i]){
total -= allnum[i];
if (i % 2 != 0)
printf("%c%c", allchar[i][0], allchar[i][1]);
else
printf("%c", allchar[i][0]);
}
}
printf("\n");
}
}
return 0;
}
int numcheck(char num1, char num2){ //計算第一個和第二個數字是多少
int pr1, pr2;
pr1 = priority(num1);
pr2 = priority(num2);
if (pr1 < pr2) return -pr1 + pr2;
else return pr1 +pr2;
}
int priority(char num){
switch (num){
case 'I':
return 1;
break;
case 'V':
return 5;
break;
case 'X':
return 10;
break;
case 'L':
return 50;
break;
case 'C':
return 100;
break;
case 'D':
return 500;
break;
case 'M':
return 1000;
break;
}
}
#include
int priority(char);
int numcheck(char, char);
void print(int);
main(){
char num1[2];
char num2[2];
while (scanf("%s%s", num1, num2) != '#'){
int sub1, sub2;//兩個數字分別的總和
sub1 = numcheck(num1[0], num1[1]);
sub2 = numcheck(num2[0], num2[1]);
int total;
if (sub1 >= sub2) total = sub1 - sub2;
else total = -sub1 + sub2;
if (total == 0) printf("ZERO");
else {
int allnum[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; //MDCLXVI
char allchar[13][2] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int i;
for (i = 0; i <= 12; i++){
while (total > allnum[i]){
total -= allnum[i];
if (i % 2 != 0)
printf("%c%c", allchar[i][0], allchar[i][1]);
else
printf("%c", allchar[i][0]);
}
}
printf("\n");
}
}
return 0;
}
int numcheck(char num1, char num2){ //計算第一個和第二個數字是多少
int pr1, pr2;
pr1 = priority(num1);
pr2 = priority(num2);
if (pr1 < pr2) return -pr1 + pr2;
else return pr1 +pr2;
}
int priority(char num){
switch (num){
case 'I':
return 1;
break;
case 'V':
return 5;
break;
case 'X':
return 10;
break;
case 'L':
return 50;
break;
case 'C':
return 100;
break;
case 'D':
return 500;
break;
case 'M':
return 1000;
break;
}
}
while (scanf("%s", num1) && num1 != '#') {
scanf("%s", num2);
...
}
應該是這樣吧