码迷,mamicode.com
首页 > 其他好文 > 详细

12. 整数转罗马数字

时间:2021-05-24 08:49:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:@param   next   罗马   func   情况   col   class   输入   count   

// 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
// 字符          数值
// I             1
// V             5
// X             10
// L             50
// C             100
// D             500
// M             1000
// 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。
// 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,
// 所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
// I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
// X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 
// C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
// 给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。

// 提示:
1 <= num <= 3999
/**
 * @param {number} num
 * @return {string}
 */

 var intToRoman = function(num) {
    const X = 10;
    const C = 100;
    const M = 1000;
    let res = ‘‘;
    let count = 1;

    let next = num;
    while(next>0){
        if(next >= M){
            count = ~~(next/M);
            res += ‘M‘.repeat(count);    
            next = next % M;
        }
        if(next >= C){
            count = ~~(next/C);
            if(count === 9){
                res += ‘CM‘
            }else if(count >= 5){
                res += ‘D‘+‘C‘.repeat(count-5);
            }else if(count === 4){
                res += ‘CD‘;
            }else{
                res += ‘C‘.repeat(count);
            }            
            next = next % C;
        }    
        if(next >= X){
            count = ~~(next/X);
            if(count === 9){
                res += ‘XC‘
            }else if(count >= 5){
                res += ‘L‘+‘X‘.repeat(count-5);
            }else if(count === 4){
                res += ‘XL‘;
            }else{
                res += ‘X‘.repeat(count);
            }      
            next = next % X;
        }  
        
        switch(next){
            case 9:
                res+=‘IX‘;
                break;
            case 8:
                res+=‘VIII‘;
                break;
            case 7:
                res+=‘VII‘;
                break;
            case 6:
                res+=‘VI‘;
                break;
            case 5:
                res+=‘V‘;
                break;
            case 4:
                res+=‘IV‘;
                break;
            case 3:
                res+=‘III‘;
                break;
            case 2:
                res+=‘II‘;
                break;
            case 1:
                res+=‘I‘;
                break;
        }
        
        if(next<X){
            next = 0;
        }
    }
    return res;
};

let num = 3999;
console.log(num, intToRoman(num))

num = 3;
console.log(num, intToRoman(num))
num = 4;
console.log(num, intToRoman(num))
num = 9;
console.log(num, intToRoman(num))
num = 10;
console.log(num, intToRoman(num))
num = 11;
console.log(num, intToRoman(num))
num = 58;
console.log(num, intToRoman(num))
num = 68;
console.log(num, intToRoman(num))
num = 598;
console.log(num, intToRoman(num))
num = 698;
console.log(num, intToRoman(num))

num = 1994;
console.log(num, intToRoman(num))

12. 整数转罗马数字

标签:@param   next   罗马   func   情况   col   class   输入   count   

原文地址:https://www.cnblogs.com/yanjianjiang/p/14767409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!