以下是程式碼
#include <iostream>
#include <cstdlib>
using namespace std;
int Rometonum(string);
string numtoRome(int);
int main()
{
string a,b;
while(cin>>a)
{
if(a=="#")
break;
cin>>b;
cout<<numtoRome(abs(Rometonum(a)-Rometonum(b)))<<endl;
}
}
int Rometonum(string s)
{
int tmp=0;
char past;
for(int i=0;i<s.length();i++)
{
if(s[i]=='M'&&past=='C')
{
tmp+=800;
continue;
}
if(s[i]=='D'&&past=='C')
{
tmp+=300;
continue;
}
if(s[i]=='C'&&past=='X')
{
tmp+=80;
continue;
}
if(s[i]=='L'&&past=='X')
{
tmp+=30;
continue;
}
if(s[i]=='X'&&past=='I')
{
tmp+=8;
continue;
}
if(s[i]=='V'&&past=='I')
{
tmp+=3;
continue;
}
switch(s[i])
{
case 'M':
tmp+=1000;
break;
case 'D':
tmp+=500;
break;
case 'C':
tmp+=100;
break;
case 'L':
tmp+=50;
break;
case 'X':
tmp+=10;
break;
case 'V':
tmp+=5;
break;
case 'I':
tmp+=1;
break;
defaut:
;
}
past=s[i];
}
return tmp;
}
string numtoRome(int a)
{
if(a==0)
return "ZERO";
string tmp;
while(a>0)
{
if(a>=1000)
{
a-=1000;
tmp=tmp+"M";
continue;
}
if(a>=900)
{
a-=900;
tmp=tmp+"CM";
continue;
}
if(a>=500)
{
a-=500;
tmp=tmp+"D";
continue;
}
if(a>=400)
{
a-=400;
tmp=tmp+"CD";
continue;
}
if(a>=100)
{
a-=100;
tmp=tmp+"C";
continue;
}
if(a>=90)
{
a-=90;
tmp=tmp+"XC";
continue;
}
if(a>=50)
{
a-=50;
tmp=tmp+"L";
continue;
}
if(a>=40)
{
a-=40;
tmp=tmp+"XL";
continue;
}
if(a>=10)
{
a-=10;
tmp=tmp+"X";
continue;
}
if(a>=9)
{
a-=9;
tmp=tmp+"IX";
continue;
}
if(a>=5)
{
a-=5;
tmp=tmp+"V";
continue;
}
if(a>=4)
{
a-=4;
tmp=tmp+"IV";
continue;
}
if(a>=1)
{
a-=1;
tmp=tmp+"I";
continue;
}
}
return tmp;
}
第三個測資
答案是ZERO
我的答案是 IV
不知道錯在哪 看了很多天了