第一測資點
答案不正確
我的答案:XIV
正確答案:XV
以下是我的程式碼,請大家幫忙
------------------------------------
#include "stdio.h"
int main(void){
char a[50],b[50];
int number;
while(scanf("%s",&a)!=EOF){
if(a[0]=='#') break;
else scanf("%s",&b);
if(trans1(a)-trans1(b)>=0) number=trans1(a)-trans1(b);
else number=trans1(b)-trans1(a);
if(number==0) printf("ZERO\n");
else trans2(number);
}
}
int trans1(char in[]){
int i,out[50],sum=0;
for(i=0;i<50;i++){
switch(in[i]){
case 'I': out[i]=1; break;
case 'V': out[i]=5; break;
case 'X': out[i]=10; break;
case 'L': out[i]=50; break;
case 'C': out[i]=100; break;
case 'D': out[i]=500; break;
case 'M': out[i]=1000; break;
default: out[i]=0;
}
}
for(i=0;i<50;i++){
if(out[i]>=out[i+1]) sum+=out[i];
else sum-=out[i];
}
return sum;
}
trans2(int m){
int i,digit[4],ten=1;
for(i=4;i>0;i--){
digit[i-1]=(m/ten)%10;
ten*=10;
}
for(i=0;i<digit[0];i++) printf("M");
if(digit[1]==9) printf("CM");
else if(digit[1]==8) printf("DCCC");
else if(digit[1]==7) printf("DCC");
else if(digit[1]==6) printf("DC");
else if(digit[1]==5) printf("D");
else if(digit[1]==4) printf("CD");
else if(digit[1]==3) printf("CCC");
else if(digit[1]==2) printf("CC");
else if(digit[1]==1) printf("C");
if(digit[2]==9) printf("XC");
else if(digit[2]==8) printf("LXXX");
else if(digit[2]==7) printf("LXX");
else if(digit[2]==6) printf("LX");
else if(digit[2]==5) printf("L");
else if(digit[2]==4) printf("XL");
else if(digit[2]==3) printf("XXX");
else if(digit[2]==2) printf("XX");
else if(digit[2]==1) printf("X");
if(digit[3]==9) printf("IX");
else if(digit[3]==8) printf("VIII");
else if(digit[3]==7) printf("VII");
else if(digit[3]==6) printf("VI");
else if(digit[3]==5) printf("V");
else if(digit[3]==4) printf("IV");
else if(digit[3]==3) printf("III");
else if(digit[3]==2) printf("II");
else if(digit[3]==1) printf("I");
printf("\n");
}
int trans1(char in[]){
int i,out[50],sum=0;
for(i=0;i<50;i++){
switch(in[i]){
case 'I': out[i]=1; break;
case 'V': out[i]=5; break;
case 'X': out[i]=10; break;
case 'L': out[i]=50; break;
case 'C': out[i]=100; break;
case 'D': out[i]=500; break;
case 'M': out[i]=1000; break;
default: out[i]=0;
}
}
for(i=0;i<50;i++){
if(out[i]>=out[i+1]) sum+=out[i];
else sum-=out[i];
}
return sum;
}
好像是沒考慮CMXCIXI等等的輸入類型