반응형
문제
https://www.acmicpc.net/problem/1158
1158번: 요세푸스 문제
첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 5,000)
www.acmicpc.net
풀이
let fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const [N, K] = fs.readFileSync(filePath).toString().split(" ").map(Number);
const people = Array.from({ length: N }, (val, idx) => idx + 1);
const answer = [];
let idx = 0;
while (people.length) {
idx = (idx + K - 1) % people.length;
answer.push(+people.splice(idx, 1));
}
console.log(`<${answer.join(", ")}>`);
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[알고리즘/백준/2798] 블랙잭 (0) | 2023.05.31 |
---|---|
[알고리즘/백준/17298] 오큰수 (0) | 2023.05.26 |
[알고리즘/백준/18258] 큐 2 (0) | 2023.05.25 |
[알고리즘/백준/9012] 괄호(스택) (0) | 2023.05.25 |
[알고리즘/백준/14940] 쉬운 최단거리(BFS, Nodejs) (1) | 2023.05.24 |