a013.
羅馬數字
--
NPSC 模擬試題
| From: [119.247.188.137] |
發表日期
:
2014-01-03 19:06
#include<iostream>
#include<string>
using namespace std;
int cal(string x)
{
if(x=="I") return 1;
if(x=="V") return 5;
if(x=="X") return 10;
if(x=="L") return 50;
if(x=="C") return 100;
if(x=="D") return 500;
if(x=="M") return 1000;
}
int arr1[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string arr2[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string to_roman(int n)
{
string roman;
int i=0;
while(n)
{
if(n>=arr1[i])
{
n-=arr1[i];
roman+=arr2[i];
}
else
i++;
}
return roman;
}
int main()
{
size_t k=0,i=0,j=0;
int num1=0,num2=0;
string a[20],b;
while(cin>>a[i])
{
if(a[i]=="#")
{
for(k=0;k<i-1;k+=2)
{
for(j=0;j<(a[k].length());j++)
{
num1+=cal(a[k].substr(j,j+1));
}
for(j=0;j<(a[k+1].length());j++)
{
num2+=cal(a[k+1].substr(j,j+1));
}
if(num1-num2==0)
{
cout<<"ZERO"<<endl;
}
else
{
cout<<to_roman(num1-num2)<<endl;
}
num1=0;
num2=0;
}
i=-1;
}
i++;
}
return 0;
}