12.整数转罗马数字

题目描述:

解法:

贪心法

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

 

posted @ 2020-01-31 20:16  thefatcat  阅读(105)  评论(0编辑  收藏  举报