整数转罗马数字,13题的镜像问题,用贪心,每次找最大的值
string intToRoman(int num) {
string symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
int i=0;
string res = "";
while(num!=0){
if(num>=value[i]){
res+=symbol[i];
num-=value[i];
}
else i++;
}
return res;
}
网友评论