티스토리 뷰

https://www.acmicpc.net/problem/11866

 

11866번: 요세푸스 문제 0

첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)

www.acmicpc.net

const fs = require('fs');
const [N,K] = fs.readFileSync("./dev/stdin").toString().trim().split(" ").map(v=>+v);


class Node{
    constructor(item){
      this.item = item;
      this.next = null;
    }
  }
  
  class Queue{
    constructor(){
      this.head = null;
      this.tail = null;
      this.length = 0;
    }
  
    push(item){
      const node = new Node(item)
      if(this.head===null){
        this.head= node;
        this.head.next = null;
      }else{
        this.tail.next = node;
      }
  
      this.tail = node;
      this.length +=1;
    }
  
    pop(){
        if(this.empty()==1) return -1
      const popItem = this.head;
      this.head = this.head.next;
      this.length -=1;
      return popItem.item;
    }
  
    size(){
      return this.length;
    }
  
    empty(){
      if(this.size()==0){
        return 1;
      }else{
        return 0;
      }
    }
  
    front(){
      if(this.empty()==1) return -1;
      return this.head.item; 
    }
  
    back(){
      if(this.empty()==1) return -1;
      return this.tail.item; 
    }
  }
  



let answer = []; 
let q = new Queue();
for(let i  =1; i<=N; i++){
    q.push(i);
}
let cnt = 1;

while(q.size()!=0){
    let temp = q.pop();
    if(cnt<K){
        q.push(temp);
        cnt++;
    }else{
        answer.push(temp);
        cnt=1;
    }
}

console.log(`<${answer.join(', ')}>`)
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함