#include<iostream>
using namespace std;
int arab(string nm,char rom[],int number[],int ct){
int a,b,c,d;
for(a=0;a<nm.length();a++){
for(b=0;b<=7;b++){
if(nm[a]==rom[b])ct+=number[b];
}
}
for(int i=0;i<(nm.length()-1);i++){
for(d=0;d<=7;d++){
if(nm[i]==rom[d])break;}
for(c=0;c<=7;c++){
if(nm[i+1]==rom[c])break;}
if(number[d]<number[c])ct-=2*number[d];
}
return ct;
}
void compare(int ct1,int ct2,int &ct){
if(ct1-ct2>0)ct=ct1-ct2;
else if(ct2>ct1)ct=ct2-ct1;
}
void roman(int ct){
switch(ct/1000){
case 0:
break;
case 1:
cout<<"M";
break;
case 2:
cout<<"MM";
break;
case 3:
cout<<"MMM";
break;
}
ct=ct%1000;
switch(ct/100){
case 1:
cout<<"C";
break;
case 2:
cout<<"CC";
break;
case 3:
cout<<"CCC";
break;
case 4:
cout<<"CD";
break;
case 5:
cout<<"D";
break;
case 6:
cout<<"DC";
break;
case 7:
cout<<"DCC";
break;
case 8:
cout<<"DCCC";
break;
case 9:
cout<<"CM";
break;
}
ct=ct%100;
switch(ct/10){
case 1:
cout<<"X";
break;
case 2:
cout<<"XX";
break;
case 3:
cout<<"XXX";
break;
case 4:
cout<<"XL";
break;
case 5:
cout<<"L";
break;
case 6:
cout<<"LX";
break;
case 7:
cout<<"LXX";
break;
case 8:
cout<<"LXXX";
break;
case 9:
cout<<"XC";
break;
}
ct=ct%10;
switch(ct){
case 1:
cout<<"I"<<endl;
break;
case 2:
cout<<"II"<<endl;
break;
case 3:
cout<<"III"<<endl;
break;
case 4:
cout<<"IV"<<endl;
break;
case 5:
cout<<"V"<<endl;
break;
case 6:
cout<<"VI"<<endl;
break;
case 7:
cout<<"VII"<<endl;
break;
case 8:
cout<<"VIII"<<endl;
break;
case 9:
cout<<"IX"<<endl;
break;
}
}
int main(){
string n,m;
while(cin>>n){
if(n!="#")cin>>m;
char rom[7]={'M','D','C','L','X','V','I'};
int number[7]={1000,500,100,50,10,5,1};
int ct1,ct2;
ct1=arab(n,rom,number,0);
ct2=arab(m,rom,number,0);
int ct;
compare(ct1,ct2,ct);
if(ct1-ct2==0){
cout<<"ZERO"<<endl;
continue;
}
roman(ct);
cout<<endl;
}
return 0;
}
我自己測試時答案都正確
但solve時:
OLE :輸出題目為要求的字:M
題目怎麼會不能輸出M?