程式碼如下
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int sa,sb,suma=0,sumb=0,sum=0,i;
char a[100],b[100];
while(scanf("%s",a)!=EOF)
{
sa=strlen(a);
if(a[0]=='#'&&a[1]=='\0')
break;
for(i=0;i<sa;i++)
{
if(a[i]=='C'&&a[i+1]=='M')
{
suma+=900;
i++;
}
else if (a[i]=='C'&&a[i+1]=='D')
{
suma+=400;
i++;
}
else if (a[i]=='X'&&a[i+1]=='C')
{
suma+=90;
i++;
}
else if (a[i]=='X'&&a[i+1]=='L')
{
suma+=40;
i++;
}
else if (a[i]=='I'&&a[i+1]=='X')
{
suma+=9;
i++;
}
else if (a[i]=='I'&&a[i+1]=='V')
{
suma+=4;
i++;
}
else if (a[i]=='M')
suma+=1000;
else if (a[i]=='D')
suma+=500;
else if (a[i]=='C')
suma+=100;
else if (a[i]=='L')
suma+=5.;
else if (a[i]=='X')
suma+=10;
else if (a[i]=='V')
suma+=5;
else if (a[i]=='I')
suma+=1;
}
scanf("%s",b);
sb=strlen(b);
for(i=0;i<sb;i++)
{
if(b[i]=='C'&&b[i+1]=='M')
{
sumb+=900;
i++;
}
else if (b[i]=='C'&&b[i+1]=='D')
{
sumb+=400;
i++;
}
else if (b[i]=='X'&&b[i+1]=='C')
{
sumb+=90;
i++;
}
else if (b[i]=='X'&&b[i+1]=='L')
{
sumb+=40;
i++;
}
else if (b[i]=='I'&&b[i+1]=='X')
{
sumb+=9;
i++;
}
else if (b[i]=='I'&&b[i+1]=='V')
{
sumb+=4;
i++;
}
else if (b[i]=='M')
sumb+=1000;
else if (b[i]=='D')
sumb+=500;
else if (b[i]=='C')
sumb+=100;
else if (b[i]=='L')
sumb+=50;
else if (b[i]=='X')
sumb+=10;
else if (b[i]=='V')
sumb+=5;
else if (b[i]=='I')
sumb+=1;
}
sum=abs(suma-sumb);
if(sum==0)
printf("ZERO\n");
else
{
while(sum>0)
{
if(sum>=1000)
{
printf("M");
sum-=1000;
}
if(sum>=900)
{
printf("CM");
sum-=900;
}
if(sum>=500)
{
printf("D");
sum-=500;
}
if(sum>=400)
{
printf("CD");
sum-=400;
}
if(sum>=100)
{
printf("C");
sum-=100;
}
if(sum>=90)
{
printf("XC");
sum-=90;
}
if(sum>=50)
{
printf("L");
sum-=50;
}
if(sum>=40)
{
printf("XL");
sum-=40;
}
if(sum>=10)
{
printf("X");
sum-=10;
}
if(sum>=9)
{
printf("IX");
sum-=9;
}
if(sum>=5)
{
printf("V");
sum-=5;
}
if(sum>=4)
{
printf("IX");
sum-=4;
}
if(sum>=1)
{
printf("I");
sum-=1;
}
}
printf("\n"); }
}
}
在codeblock上執行都ok,測試執行也ok
但送出去卻出現以下結果
第 1 測資點(100%): WA (line:3)
答案不正確
您的答案為: MCMXCVIII ...略 正確答案為: ZERO
哪位大大能幫我找一下錯誤 謝謝!!!