#include <iostream>
#include <string>
using namespace std;
int main()
{
char x[4000],y[4000];
int X=0,Y=0;
while(cin>>x){
X=0;
Y=0;
if(x[0]=='#'){
break;
}
cin>>y;
for(int i=0;i<strlen(x);i++){
if(x[i]=='I'){
X+=1;
}
if(x[i]=='V'){
X+=5;
if((i!=0)&&(x[i-1]=='I')){
X-=2;
}
}
if(x[i]=='X'){
X+=10;
if((i!=0)&&(x[i-1]=='I')){
X-=2;
}
if((i!=0)&&(x[i-1]=='V')){
X-=10;
}
}
if(x[i]=='L'){
X+=50;
if((i!=0)&&(x[i-1]=='I')){
X-=2;
}
if((i!=0)&&(x[i-1]=='V')){
X-=10;
}
if((i!=0)&&(x[i-1]=='X')){
X-=20;
}
}
if(x[i]=='C'){
X+=100;
if((i!=0)&&(x[i-1]=='I')){
X-=2;
}
if((i!=0)&&(x[i-1]=='V')){
X-=10;
}
if((i!=0)&&(x[i-1]=='X')){
X-=20;
}
if((i!=0)&&(x[i-1]=='L')){
X-=100;
}
}
if(x[i]=='D'){
X+=500;
if((i!=0)&&(x[i-1]=='I')){
X-=2;
}
if((i!=0)&&(x[i-1]=='V')){
X-=10;
}
if((i!=0)&&(x[i-1]=='X')){
X-=20;
}
if((i!=0)&&(x[i-1]=='L')){
X-=100;
}
if((i!=0)&&(x[i-1]=='C')){
X-=200;
}
}
if(x[i]=='M'){
X+=1000;
if((i!=0)&&(x[i-1]=='I')){
X-=2;
}
if((i!=0)&&(x[i-1]=='V')){
X-=10;
}
if((i!=0)&&(x[i-1]=='X')){
X-=20;
}
if((i!=0)&&(x[i-1]=='L')){
X-=100;
}
if((i!=0)&&(x[i-1]=='C')){
X-=200;
}
if((i!=0)&&(x[i-1]=='D')){
X-=1000;
}
}
}
for(int i=0;i<strlen(y);i++){
if(y[i]=='I'){
Y+=1;
}
if(y[i]=='V'){
Y+=5;
if((i!=0)&&(y[i-1]=='I')){
Y-=2;
}
}
if(y[i]=='X'){
Y+=10;
if((i!=0)&&(y[i-1]=='I')){
Y-=2;
}
if((i!=0)&&(y[i-1]=='V')){
Y-=10;
}
}
if(y[i]=='L'){
Y+=50;
if((i!=0)&&(y[i-1]=='I')){
Y-=2;
}
if((i!=0)&&(y[i-1]=='V')){
Y-=10;
}
if((i!=0)&&(y[i-1]=='X')){
Y-=20;
}
}
if(y[i]=='C'){
Y+=100;
if((i!=0)&&(y[i-1]=='I')){
Y-=2;
}
if((i!=0)&&(y[i-1]=='V')){
Y-=10;
}
if((i!=0)&&(y[i-1]=='X')){
Y-=20;
}
if((i!=0)&&(y[i-1]=='L')){
Y-=100;
}
}
if(y[i]=='D'){
Y+=500;
if((i!=0)&&(y[i-1]=='I')){
Y-=2;
}
if((i!=0)&&(y[i-1]=='V')){
Y-=10;
}
if((i!=0)&&(y[i-1]=='X')){
Y-=20;
}
if((i!=0)&&(y[i-1]=='L')){
Y-=100;
}
if((i!=0)&&(y[i-1]=='C')){
Y-=200;
}
}
if(y[i]=='M'){
Y+=1000;
if((i!=0)&&(y[i-1]=='I')){
Y-=2;
}
if((i!=0)&&(y[i-1]=='V')){
Y-=10;
}
if((i!=0)&&(y[i-1]=='X')){
Y-=20;
}
if((i!=0)&&(y[i-1]=='L')){
Y-=100;
}
if((i!=0)&&(y[i-1]=='C')){
Y-=200;
}
if((i!=0)&&(y[i-1]=='D')){
Y-=1000;
}
}
}
X-=Y;
if(X==0){
cout<<"ZERO"<<endl;
continue;
}
string k;
while(X>=1000){
X-=1000;
k+='M';
}
while(X>=500){
X-=500;
k+='D';
}
while(X>=100){
X-=100;
k+='C';
}
while(X>=50){
X-=50;
k+='L';
}
while(X>=10){
X-=10;
k+='X';
}
while(X>=5){
X-=5;
k+='V';
}
while(X>=1){
X-=1;
k+='I';
}
int c12=k.find("DCCCC", 0);
if(c12>=0){
k.erase(c12,3);
k[c12]='C';
k[c12+1]='M';
}
int c1=k.find("CCCC", 0);
if(c1>=0){
k.erase(c1,2);
k[c1]='C';
k[c1+1]='D';
}
int c22=k.find("LXXXX", 0);
if(c22>=0){
k.erase(c22,3);
k[c22]='X';
k[c22+1]='C';
}
int c2=k.find("XXXX", 0);
if(c2>=0){
k.erase(c2,2);
k[c2]='X';
k[c2+1]='L';
}
int c32=k.find("VIIII", 0);
if(c32>=0){
k.erase(c32,3);
k[c32]='I';
k[c32+1]='X';
}
int c3=k.find("IIII", 0);
if(c3>=0){
k.erase(c3,2);
k[c3]='I';
k[c3+1]='V';
}
cout<<k<<endl;
}
return 0;
}
這樣應該是不可能錯了吧??
結果:
與正確輸出不相符(line:8)
您的答案為: XXX
正確答案為: XVIII