12.整数转罗马数字

余生长醉 提交于 2020-01-31 20:58:15

题目描述:

解法:

贪心法

class Solution {
public:
    string intToRoman(int num) {
        vector<int> number = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
        vector<string> roman={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};   
        string result;
            for(int i =0;i<roman.size();i++){
                while(num>=number[i]){   //注意=,以及这里需要用while,不能用if
                    result.append(roman[i]);
                    num -= number[i];
                }
            }
        return result;
    }
};

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!