1.思路
:
1.1: 将减法得到的数字对应成单独的数字,见上图(来自力扣官网题解);
1.2: 问题转换为用13中面值不同的钞票拼成一个整数;
1.3:可以采用贪心法,每次尽量尝试用较大面值的钞票去拼凑,这样可以使用的钞票数量最少
2.代码
class Solution {
public:
string intToRoman(int num) {
int value[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string reps[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string res;
for(int i=0;i<13;i++)//贪心,从面值大的开始拼凑
{
while(num>=value[i])//注意是>=!!!!!!
{
num-=value[i];
res+=reps[i];
}
}
return res;
}
};