懒癌发作,不想码字,上代码吧。
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)
网友评论