• 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语言(超出时间限制)

    1. char * intToRoman(int num){
    2. char Roman[16];
    3. int i=0,temp;
    4. while(num)
    5. {
    6. if(num/1000>0)
    7. {
    8. temp=num/1000;
    9. while(temp)
    10. {
    11. if(temp>0&&temp<4)
    12. {
    13. Roman[i++]='M';
    14. temp--;
    15. }
    16. }
    17. if(temp==0)
    18. {
    19. num=num%1000;
    20. continue;
    21. }
    22. }
    23. if(num/100>0)
    24. {
    25. temp=num/100;
    26. while(temp)
    27. {
    28. if(temp==9)
    29. {
    30. Roman[i++]='C';
    31. Roman[i++]='M';
    32. temp=temp-9;
    33. }
    34. else if(temp>4&&temp<9)
    35. {
    36. Roman[i++]='D';
    37. while(temp>5)
    38. {
    39. Roman[i++]='C';
    40. temp--;
    41. }
    42. temp=temp-5;
    43. }
    44. else if(temp==4)
    45. {
    46. Roman[i++]='C';
    47. Roman[i++]='D';
    48. temp=temp-4;
    49. }
    50. else if(temp>0&&temp<4)
    51. {
    52. Roman[i++]='C';
    53. temp--;
    54. }
    55. }
    56. if(temp==0)
    57. {
    58. num=num%100;
    59. continue;
    60. }
    61. }
    62. if(num/10>0)
    63. {
    64. temp=num/10;
    65. while(temp)
    66. {
    67. if(temp==9)
    68. {
    69. Roman[i++]='X';
    70. Roman[i++]='C';
    71. temp=temp-9;
    72. }
    73. else if(temp>4&&temp<9)
    74. {
    75. Roman[i++]='L';
    76. while(temp>5)
    77. {
    78. Roman[i++]='X';
    79. temp--;
    80. }
    81. temp=temp-5;
    82. }
    83. else if(temp==4)
    84. {
    85. Roman[i++]='X';
    86. Roman[i++]='L';
    87. temp=temp-4;
    88. }
    89. else if(temp>0&&temp<4)
    90. {
    91. Roman[i++]='X';
    92. temp--;
    93. }
    94. }
    95. if(temp==0)
    96. {
    97. num=num%10;
    98. continue;
    99. }
    100. }
    101. if(num>0)
    102. {
    103. while(num)
    104. {
    105. if(num==9)
    106. {
    107. Roman[i++]='I';
    108. Roman[i++]='X';
    109. temp=temp-9;
    110. }
    111. else if(temp>4&&temp<9)
    112. {
    113. Roman[i++]='V';
    114. while(temp>5)
    115. {
    116. Roman[i++]='X';
    117. temp--;
    118. }
    119. temp=temp-5;
    120. }
    121. else if(temp==4)
    122. {
    123. Roman[i++]='I';
    124. Roman[i++]='V';
    125. temp=temp-4;
    126. }
    127. else if(temp>0&&temp<4)
    128. {
    129. Roman[i++]='I';
    130. temp--;
    131. }
    132. }
    133. if(temp==0)
    134. {
    135. continue;
    136. }
    137. }
    138. }
    139. return Roman;
    140. }

    C语言:

    1. char * intToRoman(int num){
    2. char* ans=(char*)malloc(16*sizeof(char));
    3. int cur=0;
    4. while(num>=1000)
    5. {
    6. ans[cur++]='M';
    7. num-=1000;
    8. }
    9. if(num>=900)
    10. {
    11. ans[cur++]='C';
    12. ans[cur++]='M';
    13. num-=900;
    14. }
    15. else if(num>=500)
    16. {
    17. ans[cur++]='D';
    18. num-=500;
    19. }
    20. else if(num>=400)
    21. {
    22. ans[cur++]='C';
    23. ans[cur++]='D';
    24. num-=400;
    25. }
    26. while(num>=100)
    27. {
    28. ans[cur++]='C';
    29. num-=100;
    30. }
    31. if(num>=90)
    32. {
    33. ans[cur++]='X';
    34. ans[cur++]='C';
    35. num-=90;
    36. }
    37. else if(num>=50)
    38. {
    39. ans[cur++]='L';
    40. num-=50;
    41. }
    42. else if(num>=40)
    43. {
    44. ans[cur++]='X';
    45. ans[cur++]='L';
    46. num-=40;
    47. }
    48. while(num>=10)
    49. {
    50. ans[cur++]='X';
    51. num-=10;
    52. }
    53. if(num>=9)
    54. {
    55. ans[cur++]='I';
    56. ans[cur++]='X';
    57. num-=9;
    58. }
    59. else if(num>=5)
    60. {
    61. ans[cur++]='V';
    62. num-=5;
    63. }
    64. else if(num>=4)
    65. {
    66. ans[cur++]='I';
    67. ans[cur++]='V';
    68. num-=4;
    69. }
    70. while(num>=1)
    71. {
    72. ans[cur++]='I';
    73. num--;
    74. }
    75. ans[cur]='\0';
    76. return ans;
    77. }

    执行结果:通过

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

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

    通过测试用例:3999 / 3999

  • 相关阅读:
    69.C++多继承与纯虚函数
    spring
    快速入门Docker
    亮相“外滩金融峰会” 百望云实力入选“融城杯金融科技创新十佳案例”
    Java异常处理笔记
    Docker compose插件安装
    Redis介绍
    esp8266 发送企业微信
    Hadoop (十五) --------- Hadoop 数据压缩
    力扣热门算法题 46-48
  • 原文地址:https://blog.csdn.net/DXB2021/article/details/125989180