LeetCode-Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
public:
string intToRoman(int num) {
string ret;
while(num>999){
ret.append("M");
num-=1000;
}
if(num>899){
ret.append("CM");
num-=900;
}
if(num>499){
ret.append("D");
num-=500;
}
if(num>399){
ret.append("CD");
num-=400;
}
while(num>99){
ret.append("C");
num-=100;
}
if(num>89){
ret.append("XC");
num-=90;
}
if(num>49){
ret.append("L");
num-=50;
}
if(num>39){
ret.append("XL");
num-=40;
}
while(num>9){
ret.append("X");
num-=10;
}
if(num>8){
ret.append("IX");
num-=9;
}
if(num>4){
ret.append("V");
num-=5;
}
if(num>3){
ret.append("IV");
num-=4;
}
while(num>0){
ret.append("I");
num-=1;
}
return ret;
}
};
浙公网安备 33010602011771号