1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/整数转罗马数字 [integer-to-roman].html
2022-03-29 12:43:11 +08:00

67 lines
2.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>罗马数字包含以下七种字符: <code>I</code> <code>V</code> <code>X</code> <code>L</code><code>C</code><code>D</code> 和 <code>M</code></p>
<pre>
<strong>字符</strong> <strong>数值</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>例如, 罗马数字 2 写做 <code>II</code> ,即为两个并列的 1。12 写做 <code>XII</code> ,即为 <code>X</code> + <code>II</code> 。 27 写做  <code>XXVII</code>, 即为 <code>XX</code> + <code>V</code> + <code>II</code> 。</p>
<p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 <code>IIII</code>,而是 <code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 <code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>
<ul>
<li><code>I</code> 可以放在 <code>V</code> (5) 和 <code>X</code> (10) 的左边,来表示 4 和 9。</li>
<li><code>X</code> 可以放在 <code>L</code> (50) 和 <code>C</code> (100) 的左边,来表示 40 和 90。 </li>
<li><code>C</code> 可以放在 <code>D</code> (500) 和 <code>M</code> (1000) 的左边来表示 400 和 900。</li>
</ul>
<p>给你一个整数,将其转为罗马数字。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> num = 3
<strong>输出:</strong> "III"</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> num = 4
<strong>输出:</strong> "IV"</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> num = 9
<strong>输出:</strong> "IX"</pre>
<p><strong>示例 4:</strong></p>
<pre>
<strong>输入:</strong> num = 58
<strong>输出:</strong> "LVIII"
<strong>解释:</strong> L = 50, V = 5, III = 3.
</pre>
<p><strong>示例 5:</strong></p>
<pre>
<strong>输入:</strong> num = 1994
<strong>输出:</strong> "MCMXCIV"
<strong>解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= num <= 3999</code></li>
</ul>