送给大家一个把阿拉伯数字与罗马数字互换的代码 -.- 仅支持4000以下的转化

本文介绍了罗马数字与阿拉伯数字之间的转换方法,并提供了相应的C++代码实现。文章详细解释了基本的转换规则,例如如何通过组合不同的罗马数字来表示特定的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本字符
I
V
X
L
C
D
M
相应的阿拉伯数字表示为
1
5
10
50
100
500
1000




1》相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;

2》小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12;

3》小的数字、(限于 Ⅰ、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;

4》正常使用时、连写的数字重复不得超过三次;

5》在一个数的上面画一条横线、表示这个数扩大 1000 倍。


阿拉伯数字化为罗马数字代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    while (n)
    {
        if (n>=1000)
        {
            while (n>=1000)
            {
                cout<<'M';
                n-=1000;
            }
        }
        else if (n>=500)
        {
            if (n>=900)
            {
                cout<<"CM";
                n-=900;
            }
            else
            {
                cout<<'D';
                n-=500;
            }
        }
        else if (n>=100)
        {
            if (n>=400)
            {
                cout<<"CD";
                n-=400;
            }
            else
            {
                cout<<'C';
                n-=100;
            }
        }
        else if (n>=50)
        {
            if (n>=90)
            {
                cout<<"XC";
                n-=90;
            }
            else
            {
                cout<<'L';
                n-=50;
            }
        }
        else if (n>=10)
        {
            if (n>=40)
            {
                cout<<"XL";
                n-=40;
            }
            else
            {
                cout<<'X';
                n-=10;
            }
        }
        else if (n>=5)
        {
            if (n>=9)
            {
                cout<<"IX";
                n-=9;
            }
            else
            {
                cout<<'V';
                n-=5;
            }
        }
        else if (n>=1)
        {
            if (n>=4)
            {
                cout<<"IV";
                n-=4;
            }
            else
            {
                cout<<'I';
                n-=1;
            }
        }
    }
    cout<<'\n';
    return 0;
}

罗马数字化为阿拉伯数字代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int hua(char xx)
{
    if (xx=='I') return 1;
    else if (xx=='V') return 5;
    else if (xx=='X') return 10;
    else if (xx=='L') return 50;
    else if (xx=='C') return 100;
    else if (xx=='D') return 500;
    else return 1000;
}
int main()
{
    char luo[100];
    int shu=0;
    cin>>luo;
    int ll=strlen(luo);
    for (int i=0;i<ll;i++)
    {
        shu+=hua(luo[i]);
        if (i&&hua(luo[i])>hua(luo[i-1]))
            shu-=2*hua(luo[i-1]);
    }
    cout<<shu<<'\n';
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值