美文网首页
5 【栈】js学习:10进制转各进制的实现

5 【栈】js学习:10进制转各进制的实现

作者: 狩秋之人 | 来源:发表于2019-11-08 14:03 被阅读0次

懒癌发作,不想码字,上代码吧。

let stuckHome = require('./stuck.js')

function divideBy2 (num) {

    let stuck = new stuckHome(),
        result = '';

    // 将二进制得到结果入栈
    while (num != 0) {
        num % 2 == 1? stuck.pushElement(1): stuck.pushElement(0)
        num = parseInt(num /= 2);
    }

    // 顺序出栈并组合成String
    while(stuck.size() !== 0) {
        result += stuck.popElement().toString();
    } 
    
    console.log(result);
}

// 测试
divideBy2 (5)

相关文章

网友评论

      本文标题:5 【栈】js学习:10进制转各进制的实现

      本文链接:https://www.haomeiwen.com/subject/tmgibctx.html