罗马数字和阿拉伯数字转换
#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;
}
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步