- 十进制转二进制
<script>
let num=18279;
console.log(num.toString(2));
</script>
- 栈——十进制转二进制
<script>
class Stack{
container=[];
enter(value){
this.container.unshift(value);
}
leave(){
return this.container.shift();
}
size(){
return this.container.length;
}
value(){
return this.container.slice(0);
}
}
Number.prototype.decimal2binary=function decimal2binary(){
let decimal=this,
sk=new Stack;
if(decimal===0) return '0';
while(decimal>0){
let n=Math.floor(decimal/2),
m=decimal%2;
sk.enter(m);
decimal=n;
}
return sk.value().join('');
};
let num=1893;
console.log(num.decimal2binary());
</script>
- 优先级队列
<script>
class Queue{
container=[];
enter(element,priority=0){
let obj={
element,
priority
}
if(priority===0){
this.container.push(obj);
return;
}
let flag=false;
for(let i=this.container.length-1;i>=0;i--){
let item=this.container[i];
if(item.priority>=priority){
this.container.splice(i-1,0,obj);
flag=true;
break;
}
}
!flag?this.container.push(obj):null;
}
leave(){
return this.container.shift();
}
size(){
return this.container.length;
}
value(){
return this.container.slice(0);
}
}
</script>
- 队列——击鼓传花
<script>
class Queue{
container=[];
enter(element){
this.container.push(element);
}
leave(){
return this.container.shift();
}
size(){
return this.container.length;
}
value(){
return this.container.slice(0);
}
}
function game(n,m){
let qe=new Queue;
for(let i=0;i<n;i++){
qe.enter(i+1);
}
while(qe.size()>1){
for(let i=0;i<m-1;i++){
qe.enter(qe.leave());
}
qe.leave();
}
return qe.value().toString();
}
console.log(game(8,5));
</script>