a013.
羅馬數字
--
NPSC 模擬試題
| From: [60.246.231.32] |
發表日期
:
2012-12-21 14:02
#include <iostream>
#include <string>
using namespace std;
int i,j,n,p,k;
string m,d;
int rome(string m)
{
n=0;
i=m.length()-1;
while (i!=-1) {
if (m[i]=='M') {if(m[i-1]=='C'){n=n+900;i=i-2;} else {n=n+1000;i=i-1;}}
if (m[i]=='D') {if(m[i-1]=='C'){n=n+400;i=i-2;} else {n=n+500;i=i-1;}}
if (m[i]=='C') {if(m[i-1]=='X'){n=n+90;i=i-2;} else {n=n+100;i=i-1;}}
if (m[i]=='L') {if(m[i-1]=='X'){n=n+40;i=i-2;} else {n=n+50;i=i-1;}}
if (m[i]=='X') {if(m[i-1]=='I'){n=n+9;i=i-2;} else {n=n+10;i=i-1;}}
if (m[i]=='V') {if(m[i-1]=='I'){n=n+4;i=i-2;} else {n=n+5;i=i-1;}}
if (m[i]=='I') {n=n+1;i=i-1;}
}
return(n);
}
void numro(int k)
{
int t;
if (k==0){cout<<"ZERO";}
if (k>=1000){t=k/1000; for (i=1;i<=t;i++){cout<<'M';}k=k%1000;}
if (k>=500){if (k>=900){cout<<"CM";k=k-900;} else {cout<<'D';k=k-500;}}
if (k>=100){if (k>=400){cout<<"CD";k=k-400;}else {t=k/100; for (i=1;i<=t;i++){cout<<'C';}k=k%100;}}
if (k>=50){if (k>=90){cout<<"XC";k=k-90;}else{cout<<'L';k=k-50;}}
if (k>=10){if (k>=40) {cout<<"XL";k=k-40;}else {t=k/10; for (i=1;i<=t;i++){cout<<'X';}k=k%10;}}
if (k>=5){if (k==9){cout<<"IX";k=k-9;}; if (k>=5){cout<<'V';k=k-5;}}
if (k==4){cout<<"IV";k=k-4;}
for (i=1;i<=k;i++){cout<<"I";}
}
int main()
{
while(cin>>m)
{
if (m!="#")
{cin>>d;
numro(abs(rome(m)-rome(d)));}
cout<<endl;
}
}