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

在C语言中,将整数换为罗马数字可以分为以下几个步骤: 1. 定义罗马数字的字符映射表,存储各个数值对应的罗马数字字符,如1为'I', 4为'IV', 5为'V', 9为'IX', 等等。 2. 定义一个函数`int_to_roman(int num)`,接受一个正整数作为输入。 3. 使用循环结构,从大到小遍历罗马数字的字符映射表。对于每个字符及其对应值,检查当前数字是否大于等于这个值,如果是,则添加相应的罗马数字字符,并从原数减去该值。 4. 如果当前值小于当前数字,就需要处理特殊的组合规则。例如,如果当前是9,而前面的是4,那么会添加'IV'而不是'I'两次。 5. 当遍历完所有可能的字符和它们对应的值后,如果还有剩余的数字未换,说明输入的不是一个有效的罗马数字范围内的整数,需要特殊处理(通常返回错误提示或特定表示非罗马数字的字符串)。 6. 函数结束时,返回换后的罗马数字字符串。 下面是一个简单的C语言示例,注意这只是基本的实现,实际项目中可能需要考虑边界条件、异常处理和性能优化: ```c #include <stdio.h> #include <string.h> char* intToRoman(int num) { // ... (罗马数字字符映射表) const char* roman_numerals = "MCMXCIV"; const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; const char* symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; if (num <= 0) return "Invalid input"; int i; char* result = malloc(sizeof(char) * 20); // 预留足够空间 for (i = 0; values[i] > 0 && num >= values[i]; i++) { result[strlen(result)] = symbols[i]; num -= values[i]; } return result; } int main() { int num = 2023; char* roman_num = intToRoman(num); printf("Integer %d in Roman numerals is %s\n", num, roman_num); free(roman_num); // 不忘记释放内存 return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值