# 12. Integer to Roman

## 1.問題

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LLjWXvDGBCXZdyo8Tty%2F-LL_11af91PpyRV7GTTB%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-09-04%20%E4%B8%8B%E5%8D%8810.41.21.png?alt=media\&token=84e82fd7-b327-42b7-8495-e0bd05743645)

## 2.想法

* 提問
* function header, parameter
* test input
* 說明想法
  * 用ordered\_map儲存個, 十, 百, 位數
  * 針對個, 十, 百, 位數mapping到對應的值
* 測試計算複雜度: O(n)

## **3.程式碼**

```
class Solution {
public:
    string intToRoman(int num) {
        unordered_map <int, string>m;
        string s = "";
        m[1] = "I";
        m[2] = "II";
        m[3] = "III";
        m[4] = "IV";
        m[5] = "V";
        m[6] = "VI";
        m[7] = "VII";
        m[8] = "VIII";
        m[9] = "IX";
        m[10] = "X";
        m[20] = "XX";
        m[30] = "XXX";
        m[40] = "XL";
        m[50] = "L";
        m[60] = "LX";
        m[70] = "LXX";
        m[80] = "LXXX";
        m[90] = "XC";
        m[100] = "C";
        m[200] = "CC";
        m[300] = "CCC";
        m[400] = "CD";
        m[500] = "D";
        m[600] = "DC";
        m[700] = "DCC";
        m[800] = "DCCC";
        m[900] = "CM";
        m[1000] = "M";
        m[2000] = "MM";
        m[3000] = "MMM";
        int i = 0;
        while (num > 0) {
            int n = num % 10;
            s = m[n * int(pow(10, i))] + s;
            i ++;
            num = num / 10;
        }
        return s;
    }
};
```

