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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值