题解 | #二进制转换#

二进制转换

http://www.nowcoder.com/practice/7b74386695cc48349af37196f45e62a8

二进制转换

给定数字除2,将余数保存到栈中(用数组模拟),最后将数组反转输出为字符串。

// 创建长度为8的数组
    let result = [];
    let rem;
    while (num > 0) {
        rem = Math.floor(num % 2);
        result.push(rem)
        num = Math.floor(num / 2);
    }
    // 不足8位补齐
    for(let i=0; i<8; i++){
        if(result[i]==undefined) {
            result[i] = 0;
        }
    }

    // 反转输出
    return result.reverse().join('');
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:21
被夸真的超级开心,好可爱的姐姐
码农索隆:老色批们不用脑补了,我把金智妮的图找来了查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务