12. Integer to Roman整数转罗马数字

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。

Symbol 字符      Value数值
I                         1
V                       5
X                     10 
L                     50
C                     100
D                     500
M                     1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

I can be placed before V (5) and X (10) to make 4 and 9. 

I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X can be placed before L (50) and C (100) to make 40 and 90. 

X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 
C can be placed before D (500) and M (1000) to make 400 and 900.

C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
Given an integer, convert it to a roman numeral.

给你一个整数,将其转为罗马数字。

Example 1:示例 1:

Input: num = 3                输入: num = 3
Output: "III"                        输出: "III"
Explanation: 3 is represented as 3 ones.


Example 2:

Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.


Example 3:

Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

示例 2:

输入: num = 4
输出: "IV"


示例 3:

输入: num = 9
输出: "IX"


示例 4:

输入: num = 58
输出: "LVIII"
解释: L = 50, V = 5, III = 3.


示例 5:

输入: num = 1994
输出: "MCMXCIV"
解释: M = 1000, CM = 900, XC = 90, IV = 4.
 

Constraints:提示:

1 <= num <= 3999

C语言(超出时间限制)

char * intToRoman(int num){
    char Roman[16];
    int i=0,temp;
    while(num)
    {
        if(num/1000>0)
        {
            temp=num/1000;
            while(temp)
            {
                if(temp>0&&temp<4)
                {
                    Roman[i++]='M';
                    temp--;
                }
            }
            if(temp==0)
            {
                num=num%1000;
                continue;
            }
        }
        if(num/100>0)
        {
            temp=num/100;
            while(temp)
            {
                if(temp==9)
                {
                    Roman[i++]='C';
                    Roman[i++]='M';
                    temp=temp-9;
                }
                else if(temp>4&&temp<9)
                {
                    Roman[i++]='D';
                    while(temp>5)
                    {
                        Roman[i++]='C';
                        temp--;
                    }
                    temp=temp-5;
                }
                else if(temp==4)
                {
                    Roman[i++]='C';
                    Roman[i++]='D';
                    temp=temp-4;
                }
                else if(temp>0&&temp<4)
                {
                    Roman[i++]='C';
                    temp--;
                }
            }
            if(temp==0)
            {
                num=num%100;
                continue;
            }
        }
        if(num/10>0)
        {
            temp=num/10;
            while(temp)
            {
                if(temp==9)
                {
                    Roman[i++]='X';
                    Roman[i++]='C';
                    temp=temp-9;
                }
                else if(temp>4&&temp<9)
                {
                    Roman[i++]='L';
                    while(temp>5)
                    {
                        Roman[i++]='X';
                        temp--;
                    }
                    temp=temp-5;
                }
                else if(temp==4)
                {
                    Roman[i++]='X';
                    Roman[i++]='L';
                    temp=temp-4;
                }
                else if(temp>0&&temp<4)
                {
                    Roman[i++]='X';
                    temp--;
                }
            }
            if(temp==0)
            {
                num=num%10;
                continue;
            }
        }
        if(num>0)
        {
            while(num)
            {
                if(num==9)
                {
                    Roman[i++]='I';
                    Roman[i++]='X';
                    temp=temp-9;
                }
                else if(temp>4&&temp<9)
                {
                    Roman[i++]='V';
                    while(temp>5)
                    {
                        Roman[i++]='X';
                        temp--;
                    }
                    temp=temp-5;
                }
                else if(temp==4)
                {
                    Roman[i++]='I';
                    Roman[i++]='V';
                    temp=temp-4;
                }
                else if(temp>0&&temp<4)
                {
                    Roman[i++]='I';
                    temp--;
                }
            }
            if(temp==0)
            {
                continue;
            }
        }        
    }
    return Roman;

}

C语言:

char * intToRoman(int num){
    char* ans=(char*)malloc(16*sizeof(char));
    int cur=0;
    while(num>=1000)
    {
        ans[cur++]='M';
        num-=1000;
    }
    if(num>=900)
    {
        ans[cur++]='C';
        ans[cur++]='M';
        num-=900;
    }
    else if(num>=500)
    {
        ans[cur++]='D';
        num-=500;
    }
    else if(num>=400)
    {
        ans[cur++]='C';
        ans[cur++]='D';
        num-=400;
    }
    while(num>=100)
    {
        ans[cur++]='C';
        num-=100;
    }
    if(num>=90)
    {
        ans[cur++]='X';
        ans[cur++]='C';
        num-=90;
    }
    else if(num>=50)
    {
        ans[cur++]='L';
        num-=50;
    }
    else if(num>=40)
    {
        ans[cur++]='X';
        ans[cur++]='L';
        num-=40;
    }
    while(num>=10)
    {
        ans[cur++]='X';
        num-=10;
    }
    if(num>=9)
    {
        ans[cur++]='I';
        ans[cur++]='X';
        num-=9;
    }
    else if(num>=5)
    {
        ans[cur++]='V';
        num-=5;
    }
    else if(num>=4)
    {
        ans[cur++]='I';
        ans[cur++]='V';
        num-=4;
    }
    while(num>=1)
    {
        ans[cur++]='I';
        num--;
    }
    ans[cur]='\0';
    return ans;
}

执行结果:通过

执行用时:8 ms, 在所有 C 提交中击败了37.27%的用户

内存消耗:5.7 MB, 在所有 C 提交中击败了83.05%的用户

通过测试用例:3999 / 3999

### 整数罗马数字 为了实现将整数换成罗马数字的功能,采用贪心算法是一个直观而有效的方式[^1]。此方法的关键在于每次尝试选取能表达的最大数值所对应的罗马字符,并从原始数值中扣除这部分值;这一操作持续执行直至输入的整数降为零。 具体而言,当面对一个待化的具体数值时,程序会不断查找不大于当前剩余数值的最大可能的罗马符号组合,随后减少相应的阿拉伯数值并累积构成最终结果串的一部分[^2]。整个流程涉及到了一系列预定义好的映射关系——即特定阿拉伯数字与其对应罗马记号之间的关联表。 考虑到罗马计数体系的特点以及其特殊规则(比如4和9这类特殊情况),解决方案还应特别注意处理这些例外情况下的模式匹配问题[^4]。因此,在设计具体的编码逻辑之前,有必要准备一份详尽的价值对照列表用于指导后续的操作: | Integer | Roman Numeral | |---------|---------------| | 1000 | M | | 900 | CM | | 500 | D | | 400 | CD | | 100 | C | | 90 | XC | | 50 | L | | 40 | XL | | 10 | X | | 9 | IX | | 5 | V | | 4 | IV | | 1 | I | 以下是基于上述原理编写的Python函数示例,它实现了完整的整数罗马数字换功能: ```python def int_to_roman(num): value_symbols = [ (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I") ] roman_numeral = "" for value, symbol in value_symbols: while num >= value: num -= value roman_numeral += symbol return roman_numeral ``` 这段代码按照预先设定好的价值-符号对数组`value_symbols`来进行迭代检查,每当发现某个元素的第一项小于等于目标整数,则立即将相应数量的第二项追加到输出字符串后面,并更新剩余需处理的部分直到完成全部换工作[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值