程式語言 - LeetCode - C++ - 12. Integer to Roman



參考資訊:
https://algo.monster/liteproblems/12

題目:


解答:

class Solution {
public:
    string intToRoman(int num) {
        string r;
        vector<string> str = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        vector<int> dec = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };

        for (int i = 0; i < str.size(); ++i) {
            while (num >= dec[i]) {
                num -= dec[i];
                r += str[i];
            }
        }

        return r;
    }
};