第 1 測資點(100%): WA (line:5)
答案不正確
您的答案為: XIV 正確答案為: XV
下面是我的程式
求好心的大大幫我看一下QQ
==============
#include<stdio.h>
const int signNum[]= {1000,500,100,50,10,5,1}; //定義羅馬數字
const char sign[]="MDCLXVI";
int roma(char);
void print(int,int);
int main()
{
char num1[50];
char num2[50];
int number[7];
int i,j,k;
int n1,n2,sum;//計數器
while(1)
{
scanf("%s",num1);
if (num1[0]=='#')
break;
scanf("%s",num2);
n1=0;
n2=0;
for(i=0; i<50; i++)
{
for (j=1; j<7; j++)
{
if (num1[i]==sign[j] && num1[i+1]==sign[j-1])
{
n1+=roma(num1[i+1])-roma(num1[i]);
i++;
break;
}
else if(num1[i]==sign[j] && num1[i+1]==sign[j-2])
{
n1+=roma(num1[i+1])-roma(num1[i]);
i++;
break;
}
else if (j==6)
{
n1+=roma(num1[i]);
}
}
}
for(i=0; i<50; i++)
{
for (j=1; j<7; j++)
{
if (num2[i]==sign[j]&& num2[i+1]==sign[j-1])
{
n2+=roma(num2[i+1])-roma(num2[i]);
i++;
break;
}
else if(num2[i]==sign[j] && num2[i+1]==sign[j-2])
{
n2+=roma(num2[i+1])-roma(num2[i]);
i++;
break;
}
else if (j==6)
{
n2+=roma(num2[i]);
}
}
}
//============================== 輸入及轉換為數字
sum=abs(n1-n2);
if (sum==0)
printf("ZERO");
else
{
for (i=0; i<7; i++)
{
number[i]=sum/signNum[i];
sum%=signNum[i];
}
for (i=1; i<7; i+=2)
{
if (number[i]+number[i+1]==5)
{
number[i+1]+=5;
number[i]=0;
}
}
for(i=0; i<7; i++)
{
print(i,number[i]);
}
}
printf("\n");
}
return 0;
}
int roma(char c)
{
switch(c)
{
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;
default:
return 0;
}
}
void print(int s,int n)
{
int i;
if (n>=9)
{
printf("%c%c",sign[s],sign[s-2]);
n-=9;
}
else if (n>=4)
{
printf("%c%c",sign[s],sign[s-1]);
n-=4;
}
for (i=1; i<=n; i++)
{
printf("%c",sign[s]);
}
}