欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

算法-栈-十进制转二进制

程序员文章站 2022-07-15 09:35:52
...
class Stack {
  items = []
  push = (e) => {
    this.items.push(e)
  }
  pop = () => {
    return this.items.pop()
  }
  peek = () => {
    return this.items[this.items.length - 1]
  }
  isEmpty = () => {
    return this.items.length === 0
  }
  size = () => {
    return this.items.length
  }
  toString = () => {
    return this.items.join('')
  }
}

let stack = new Stack()


//十进制转二进制
test = (x) => {
  const a = x % 2
  stack.push(a)
  const aa = parseInt(x / 2, 10)
  if (aa !== 0) {
    test(aa)
  }
}
test(1000)
console.log('====www===', stack.toString())
相关标签: 算法 算法