题目描述:
解法:
贪心法
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; } };
来源:https://www.cnblogs.com/thefatcat/p/12246566.html