solution.md 6.6 KB
Newer Older
1
# 整数转罗马数字
F
fix bug  
feilong 已提交
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
<div class="notranslate">
    <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>
    </p>

    <pre><strong>字符</strong>          <strong>数值</strong>
I             1
V             5
X             10
L             50
C             100
D             500
M             1000</pre>

    <p>例如, 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;,即为两个并列的 1。12
        写做&nbsp;<code>XII</code>&nbsp;,即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27
        写做&nbsp;&nbsp;<code>XXVII</code>,
        即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。</p>

    <p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做&nbsp;<code>IIII</code>,而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5
        减小数 1 得到的数值 4 。同样地,数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>

    <ul>
        <li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边,来表示 4 和 9。</li>
        <li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边,来表示 40
            和&nbsp;90。&nbsp;</li>
        <li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边,来表示&nbsp;400
            和&nbsp;900。</li>
    </ul>

    <p>给你一个整数,将其转为罗马数字。</p>

    <p>&nbsp;</p>

    <p><strong>示例&nbsp;1:</strong></p>

    <pre><strong>输入:</strong>&nbsp;num = 3
<strong><br />输出:</strong> "III"</pre>

    <p><strong>示例&nbsp;2:</strong></p>

    <pre><strong>输入:</strong>&nbsp;num = 4
<strong><br />输出:</strong> "IV"</pre>

    <p><strong>示例&nbsp;3:</strong></p>

    <pre><strong>输入:</strong>&nbsp;num = 9
<strong><br />输出:</strong> "IX"</pre>

    <p><strong>示例&nbsp;4:</strong></p>

    <pre><strong>输入:</strong>&nbsp;num = 58
<strong><br />输出:</strong> "LVIII"
<strong><br />解释:</strong> L = 50, V = 5, III = 3.
    </pre>

    <p><strong>示例&nbsp;5:</strong></p>

    <pre><strong>输入:</strong>&nbsp;num = 1994
<strong><br />输出:</strong> "MCMXCIV"
<strong><br />解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>

    <p>&nbsp;</p>

    <p><strong>提示:</strong></p>

    <ul>
        <li><code>1 &lt;= num &lt;= 3999</code></li>
    </ul>
</div>
每日一练社区's avatar
每日一练社区 已提交
72
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
73

74
## aop
F
fix bug  
feilong 已提交
75

76
### before
F
fix bug  
feilong 已提交
77

78 79 80 81 82
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
83

84 85 86 87 88 89 90 91 92 93 94
```cpp
int main()
{
    Solution sol;
    cout << sol.intToRoman(1994) << endl;
    return 0;
}

```

## 答案
F
fix bug  
feilong 已提交
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
```cpp
class Solution
{
public:
    string intToRoman(int num)
    {
        int nums[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        string romans[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        string ans;
        int index = 0;
        while (index < 13)
        {
            while (num >= nums[index])
            {
                num = nums[index];
                ans = romans[index];
            }
            index++;
        }
        return ans;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
121

122
### A
F
fix bug  
feilong 已提交
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
```cpp
class Solution
{
public:
    string getNum1000(int num1000)
    {
        if (num1000 == 3)
        {
            return "MMM";
        }
        else if (num1000 == 2)
        {
            return "MM";
        }
        else if (num1000 == 1)
        {
            return "M";
        }
        else
        {
            return "";
        }
    }

    string getNum100(int num100)
    {

        switch (num100)
        {
        case 1:
            return "C";
        case 2:
            return "CC";
        case 3:
            return "CCC";
        case 4:
            return "CD";
        case 5:
            return "D";
        case 6:
            return "DC";
        case 7:
            return "DCC";
        case 8:
            return "DCCC";
        case 9:
            return "CM";
        default:
            return "";
        }
    }

    string getNum10(int num10)
    {

        switch (num10)
        {
        case 1:
            return "X";
        case 2:
            return "XX";
        case 3:
            return "XXX";
        case 4:
            return "XL";
        case 5:
            return "L";
        case 6:
            return "LX";
        case 7:
            return "LXX";
        case 8:
            return "LXXX";
        case 9:
            return "XC";
        default:
            return "";
        }
    }

    string getNum1(int num1)
    {
        switch (num1)
        {
        case 1:
            return "I";
        case 2:
            return "II";
        case 3:
            return "III";
        case 4:
            return "IV";
        case 5:
            return "V";
        case 6:
            return "VI";
        case 7:
            return "VII";
        case 8:
            return "VIII";
        case 9:
            return "IX";
        default:
            return "";
        }
    }

    string intToRoman(int num)
    {
        int num1000 = num / 1000;
        int num100 = (num % 1000) / 100;
        int num10 = (num % 100) / 10;
        int num1 = (num % 10);
        string res;
        res = getNum1000(num1000) + getNum100(num100) + getNum10(num10) + getNum1(num1);
        return res;
    }
};
```

### B
F
fix bug  
feilong 已提交
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
```cpp
class Solution
{
public:
    string intToRoman(int num)
    {
        int values[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        string reps[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        string res;
        for (int i = 0; i < 13; i++)
        {
            while (num >= values[i])
            {
                num -= values[i];
                res += reps[i];
            }
        }
        return res;
    }
};
```

### C
F
fix bug  
feilong 已提交
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283
```cpp
class Solution
{
public:
    string intToRoman(int num)
    {
        string bit1[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
        string bit10[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
        string bit100[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
        string bit1000[4] = {"", "M", "MM", "MMM"};
        return bit1000[num / 1000] + bit100[num % 1000 / 100] + bit10[num % 100 / 10] + bit1[num % 10];
    }
};
```