# 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;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/12.-integer-to-roman.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
