罗马数字和阿拉伯数字转换
#include <string>
using namespace std;
int mp[100];
int val[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string r[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
void init()
{
mp['I']=1;
mp['V']=5;
mp['X']=10;
mp['L']=50;
mp['C']=100;
mp['D']=500;
mp['M']=1000;
return;
}
int r2n(char *str)
{
int t,re,pre;
re=pre=mp[str[0]];
for(t=1;str[t]!='\0';t++)
{
if(mp[str[t]]<=pre)
re+=mp[str[t]];
else
re=re-2*pre+mp[str[t]];
pre=str[t];
}
return re;
}
string n2r(int k)
{
int i=0;
string re="";
while(k>0)
{
while(k>=a[i])
{
re+=r[i];
re-=a[i];
}
i++;
}
return re;
}
using namespace std;
int mp[100];
int val[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string r[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
void init()
{
mp['I']=1;
mp['V']=5;
mp['X']=10;
mp['L']=50;
mp['C']=100;
mp['D']=500;
mp['M']=1000;
return;
}
int r2n(char *str)
{
int t,re,pre;
re=pre=mp[str[0]];
for(t=1;str[t]!='\0';t++)
{
if(mp[str[t]]<=pre)
re+=mp[str[t]];
else
re=re-2*pre+mp[str[t]];
pre=str[t];
}
return re;
}
string n2r(int k)
{
int i=0;
string re="";
while(k>0)
{
while(k>=a[i])
{
re+=r[i];
re-=a[i];
}
i++;
}
return re;
}