백준 1158 조세퍼스 문제 (NodeJs)

박태준·2019년 12월 28일
0

rotate 배열을 생성하고 queue로 활용
count 변수를 생성하고 k번째까지 카운트하면서 rotate.push(shift())를 반복
k번째가 됐을 때 shift()하고 값을 answer에 문자열로 추가
마지막 배열일 때 ">" 추가하며 반복문 종료

주의사항
출력값을 문자열로 직접 만들어야함

풀이코드

let input = [];
var require = require("readline")

.createInterface(process.stdin, process.stdout)
.on("line", function(line) {
input.push(line.trim());
})
.on("close", function() {

        let rotate = [];
		let answer = "<";
		let inputSplit = input[0].split(" ");
		let m = Number(inputSplit[0]) + 1;
		let k = Number(inputSplit[1]);
		let count = 0;

		for (let i = 1; i < m; i++) {
  			rotate.push(i);
		}

		while (rotate.length) {
  			count++;
  			if (count === k) {
    			answer += rotate.shift();
    			if (rotate.length) {
      				answer += ", ";
      				count = 0;
    			} else {
      				answer += ">";
    			}
  			} else {
    			rotate.push(rotate.shift());
  			}
		}

	console.log(answer);

});

profile
js개발 블로그

0개의 댓글