{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "ca49a1a190c646e99b6681637db6af05",
  "keywords": "哈希表,数学,字符串",
  "title": "整数转罗马数字",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>罗马数字包含以下七种字符：&nbsp;<code>I</code>，&nbsp;<code>V</code>，&nbsp;<code>X</code>，&nbsp;<code>L</code>，<code>C</code>，<code>D</code>&nbsp;和&nbsp;<code>M</code>。\n</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>字符</strong>          <strong>数值</strong>\nI             1\nV             5\nX             10\nL             50\nC             100\nD             500\nM             1000</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>例如， 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;，即为两个并列的 1。12\n写做&nbsp;<code>XII</code>&nbsp;，即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27\n写做&nbsp;&nbsp;<code>XXVII</code>,\n即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>通常情况下，罗马数字中小的数字在大的数字的右边。但也存在特例，例如 4 不写做&nbsp;<code>IIII</code>，而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边，所表示的数等于大数 5\n减小数 1 得到的数值 4 。同样地，数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边，来表示 4 和 9。</li>\n<li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边，来表示 40\n和&nbsp;90。&nbsp;</li>\n<li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边，来表示&nbsp;400\n和&nbsp;900。</li>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>给你一个整数，将其转为罗马数字。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;1:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 3\n<strong><br />输出:</strong> \"III\"</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;2:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 4\n<strong><br />输出:</strong> \"IV\"</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;3:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 9\n<strong><br />输出:</strong> \"IX\"</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;4:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 58\n<strong><br />输出:</strong> \"LVIII\"\n<strong><br />解释:</strong> L = 50, V = 5, III = 3.\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;5:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 1994\n<strong><br />输出:</strong> \"MCMXCIV\"\n<strong><br />解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>1 &lt;= num &lt;= 3999</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "struct rmap\n{\n\tchar *r;\n\tint v;\n} units[] = {\n\t{\"M\", 1000},\n\t{\"CM\", 900},\n\t{\"D\", 500},\n\t{\"CD\", 400},\n\t{\"C\", 100},\n\t{\"XC\", 90},\n\t{\"L\", 50},\n\t{\"XL\", 40},\n\t{\"X\", 10},\n\t{\"IX\", 9},\n\t{\"V\", 5},\n\t{\"IV\", 4},\n\t{\"I\", 1}};\n#include <string.h>\nchar result[64];\nchar *intToRoman(int num)\n{\n\tresult[0] = 0;\n\tint ri = 0;\n\tint i = 0;\n\twhile (num)\n\t{\n\t\tif (num >= units[i].v)\n\t\t{\n\t\t\tstrcat(result, units[i].r);\n\t\t\tnum -= units[i].v;\n\t\t}\n\t\telse\n\t\t{\n\t\t\ti++;\n\t\t}\n\t}\n\treturn result;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-eb10661e86af495b8acef4aac7e1af86",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600469817"
}