치춘짱베리굿나이스
[백준] 15088 본문
Game of Throwns
문제
Daenerys frequently invents games to help teach her second grade Computer Science class about various aspects of the discipline. For this week’s lesson she has the children form a circle and (carefully) throw around a petrified dragon egg.
The n children are numbered from 0 to n − 1 (it is a Computer Science class after all) clockwise around the circle. Child 0 always starts with the egg. Daenerys will call out one of two things:
- a number t, indicating that the egg is to be thrown to the child who is t positions clockwise from the current egg holder, wrapping around if necessary. If t is negative, then the throw is to the counterclockwise direction.
- the phrase undo m, indicating that the last m throws should be undone. Note that undo commands never undo other undo commands; they just undo commands described in item 1 above.
For example, if there are 5 children, and the teacher calls out the four throw commands 8 -2 3 undo 2, the throws will start from child 0 to child 3, then from child 3 to child 1, then from child 1 to child 4. After this, the undo 2 instructions will result in the egg being thrown back from child 4 to child 1 and then from child 1 back to child 3. If Daenerys calls out 0 (or n, −n, 2n, −2n, etc.) then the child with the egg simply throws it straight up in the air and (carefully) catches it again.
Daenerys would like a little program that determines where the egg should end up if her commands are executed correctly. Don’t ask what happens to the children if this isn’t the case.
입력
Input consists of two lines. The first line contains two positive integers n k (1 ≤ n ≤ 30, 1 ≤ k ≤ 100) indicating the number of students and how many throw commands Daenerys calls out, respectively. The following line contains the k throw commands. Each command is either an integer p (−10 000 ≤ p ≤ 10 000) indicating how many positions to throw the egg clockwise or undo m (m ≥ 1) indicating that the last m throws should be undone. Daenerys never has the kids undo beyond the start of the game.
출력
Display the number of the child with the egg at the end of the game.
풀이
const daenerys = () => {
let input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");
const len = parseInt(input[0].split(" ")[0]);
let stack = [];
input = input[1].split(" ");
stack.push(0);
for (let i = 0; i < input.length; i++) {
if (input[i] === "undo") {
let iter = parseInt(input[++i]);
while (iter-- > 0) stack.pop();
} else {
let num = stack[stack.length - 1] + parseInt(input[i]);
if (num < 0) while (num < 0) num += len;
if (num >= len) while (num >= len) num -= len;
stack.push(num);
}
}
console.log(stack[stack.length - 1]);
};
daenerys();
반성회
계란을 던졌다면 해당 계란을 잡은 아이의 번호를 계산해서 (
num
) 스택에push
한다스택의 top에 있는 값 (
stack[stack.length -1]
) 이 마지막으로 계란을 잡은 아이가 된다Undo가 나왔을 경우 배열의 다음 값만큼 스택에서
pop
한다
'Javascript + Typescript > 자바스크립트로 알고리즘풀기' 카테고리의 다른 글
[백준] 2609 (0) | 2022.02.08 |
---|---|
[백준] 2751 (0) | 2022.02.08 |
[백준] 3986 (0) | 2022.02.08 |
[백준] 4949 (0) | 2022.02.08 |
[백준] 5957 (0) | 2022.02.08 |