解法一
/**
* @param {number} num
* @return {string}
*/
var intToRoman = function(num) {
let roman='';
if(Math.floor(num/1000) >=1 ){ // 比1000大
for (let i=0;i<Math.floor(num/1000);i++){
roman+='M';
}
}
if(Math.floor((num%1000)/100)>=1){ // 小于1000
if(Math.floor((num%1000)/100) === 4){
roman+='CD';
}else if(Math.floor((num%1000)/100) === 9) {
roman+='CM';
}else if(Math.floor((num%1000)/100) >= 5 && Math.floor((num%1000)/100) < 9){
roman+='D';
for(let i=0;i<Math.floor((num%1000)/100-5);i++){
roman+='C';
}
} else {
for(let j=0;j<Math.floor((num%1000)/100);j++){
roman+='C';
}
}
}
if(Math.floor(((num%1000)%100)/10) >=1){ // 小于100
if(Math.floor(((num%1000)%100)/10) === 4){
roman+='XL';
}else if(Math.floor(((num%1000)%100)/10) === 9){
roman+='XC';
}else if(Math.floor(((num%1000)%100)/10) >= 5 && Math.floor(((num%1000)%100)/10) < 9){
roman+='L';
for(let i=0;i< Math.floor(((num%1000)%100)/10-5);i++){
roman+='X';
}
}else {
for(let i=0;i<Math.floor(((num%1000)%100)/10);i++){
roman+='X';
}
}
}
if(Math.floor(((num%1000)%100)%10) >= 1){ //小于10
if(Math.floor(((num%1000)%100)%10) === 4){
roman+='IV';
}else if(Math.floor(((num%1000)%100)%10) === 9){
roman+='IX';
}else if(Math.floor(((num%1000)%100)%10) >= 5 && Math.floor(((num%1000)%100)%10) < 9){
roman+='V';
for(let i=0;i<Math.floor(((num%1000)%100)%10-5);i++){
roman+='I';
}
}else {
for(let m=0;m<Math.floor(((num%1000)%100)%10);m++){
roman+='I';
}
}
}
return roman;
};
思路:依次处理>1000,<1000,<100,<10的情况,在<1000,<100,<10的情况下分别处理 1=<x<=3 ,===4,5=<x<9, ===9 的情况~