羅馬數字相減那題,他說我的答案是XXVIII,正確解答是XVIII。
似乎是純粹的計算錯誤...
可是我找不到問題,我也試了幾組答案是18的測資,都有算對。
各位大神能幫我看看問題出在哪裡嗎?
#include<iostream>
using namespace std;
int main(){
string a,b;
while(cin>>a>>b){
if(a=="#")break;
int al=a.size();
int bl=b.size();
int adig[4]={0,0,0,0};
int bdig[4]={0,0,0,0};
for(int c=0;c<al;c++){
if(a[c]=='M')adig[0]+=1;
if(a[c]=='D')adig[1]+=5;
if(a[c]=='C')adig[1]+=1;
if(a[c]=='L')adig[2]+=5;
if(a[c]=='X')adig[2]+=1;
if(a[c]=='V')adig[3]+=5;
if(a[c]=='I')adig[3]+=1;
if(a[c]=='C'&&a[c+1]=='M'){adig[1]=9;c+=1;}
if(a[c]=='C'&&a[c+1]=='M'){adig[1]=5;c+=1;}
if(a[c]=='X'&&a[c+1]=='C'){adig[2]=9;c+=1;}
if(a[c]=='X'&&a[c+1]=='L'){adig[2]=5;c+=1;}
if(a[c]=='I'&&a[c+1]=='X'){adig[3]=9;c+=1;}
if(a[c]=='I'&&a[c+1]=='V'){adig[3]=5;c+=1;}
}
for(int c=0;c<bl;c++){
if(b[c]=='M')bdig[0]+=1;
if(b[c]=='D')bdig[1]+=5;
if(b[c]=='C')bdig[1]+=1;
if(b[c]=='L')bdig[2]+=5;
if(b[c]=='X')bdig[2]+=1;
if(b[c]=='V')bdig[3]+=5;
if(b[c]=='I')bdig[3]+=1;
if(b[c]=='C'&&b[c+1]=='M'){bdig[1]=9;c+=1;}
if(b[c]=='C'&&b[c+1]=='M'){bdig[1]=5;c+=1;}
if(b[c]=='X'&&b[c+1]=='C'){bdig[2]=9;c+=1;}
if(b[c]=='X'&&b[c+1]=='L'){bdig[2]=5;c+=1;}
if(b[c]=='I'&&b[c+1]=='X'){bdig[3]=9;c+=1;}
if(b[c]=='I'&&b[c+1]=='V'){bdig[3]=5;c+=1;}
}
int A=1000*adig[0]+100*adig[1]+10*adig[2]+adig[3];
int B=1000*bdig[0]+100*bdig[1]+10*bdig[2]+bdig[3];
int ans=A-B;
if(ans<0)ans*=-1;
if(!ans)cout<<"ZERO"<<endl;
else{
int Ans[4]={0,0,0,0};
for(int c=0;c<4;c++){
Ans[3-c]=ans%10;
ans/=10;
}
switch(Ans[0]){
case 0:
break;
case 3:
cout<<"M";
case 2:
cout<<"M";
case 1:
cout<<"M";
}
switch(Ans[1]){
case 0:
break;
case 9:
cout<<"CM";
break;
case 8:
cout<<"DCCC";
break;
case 7:
cout<<"DCC";
break;
case 6:
cout<<"DC";
break;
case 5:
cout<<"D";
break;
case 4:
cout<<"CD";
break;
case 3:
cout<<"CCC";
break;
case 2:
cout<<"CC";
break;
case 1:
cout<<"C";
break;
}
switch(Ans[2]){
case 0:
break;
case 9:
cout<<"XC";
break;
case 8:
cout<<"LXXX";
break;
case 7:
cout<<"LXX";
break;
case 6:
cout<<"LX";
break;
case 5:
cout<<"L";
break;
case 4:
cout<<"XL";
break;
case 3:
cout<<"XXX";
break;
case 2:
cout<<"XX";
break;
case 1:
cout<<"X";
break;
}
switch(Ans[3]){
case 0:
break;
case 9:
cout<<"IX";
break;
case 8:
cout<<"VIII";
break;
case 7:
cout<<"VII";
break;
case 6:
cout<<"VI";
break;
case 5:
cout<<"V";
break;
case 4:
cout<<"IV";
break;
case 3:
cout<<"III";
break;
case 2:
cout<<"II";
break;
case 1:
cout<<"I";
break;
}
cout<<endl;
}
}
return 0;
}