728x90
2161번: 카드1
N장의 카드가 있다. 각각의 카드는 차례로 1부터 N까지의 번호가 붙어 있으며, 1번 카드가 제일 위에, N번 카드가 제일 아래인 상태로 순서대로 카드가 놓여 있다. 이제 다음과 같은 동작을 카드가
www.acmicpc.net
문제
N장의 카드가 있다. 각각의 카드는 차례로 1부터 N까지의 번호가 붙어 있으며, 1번 카드가 제일 위에, N번 카드가 제일 아래인 상태로 순서대로 카드가 놓여 있다.
이제 다음과 같은 동작을 카드가 한 장 남을 때까지 반복하게 된다. 우선, 제일 위에 있는 카드를 바닥에 버린다. 그 다음, 제일 위에 있는 카드를 제일 아래에 있는 카드 밑으로 옮긴다.
예를 들어 N=4인 경우를 생각해 보자. 카드는 제일 위에서부터 1234 의 순서로 놓여있다. 1을 버리면 234가 남는다. 여기서 2를 제일 아래로 옮기면 342가 된다. 3을 버리면 42가 되고, 4를 밑으로 옮기면 24가 된다. 마지막으로 2를 버리고 나면, 버린 카드들은 순서대로 1 3 2가 되고, 남는 카드는 4가 된다.
N이 주어졌을 때, 버린 카드들을 순서대로 출력하고, 마지막에 남게 되는 카드를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 정수 N(1 ≤ N ≤ 1,000)이 주어진다.
출력
첫째 줄에 버리는 카드들을 순서대로 출력한다. 제일 마지막에는 남게 되는 카드의 번호를 출력한다.
예제 입력 1 복사
7
예제 출력 1 복사
1 3 5 7 4 2 6
Solve
- 자바스크립트의 Array 메서드로도 해결할 수 있지만 단일 연결리스트 자료구조를 이용하여 문제를 풀어보았습니다.
- 문제의 조건이 가장 앞의 원소와 가장 뒤의 원소만 다루기 때문에 연결리스트 자료구조가 적합하다고 생각하였습니다.
Code
class Node {
constructor(val) {
this.val = val;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(val) {
const newNode = new Node(val);
if(!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.length++;
return this;
}
pop() {
if(!this.head) return undefined;
const poppedNode = this.tail;
if(this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.tail = poppedNode.prev;
this.tail.next = null;
poppedNode.prev = null;
}
this.length--;
return poppedNode;
}
shift() {
if(this.length === 0) return undefined;
const oldHead = this.head;
if(this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.head = oldHead.next;
this.head.prev = null;
oldHead.next = null;
}
this.length--;
return oldHead;
}
unshift(val) {
const newNode = new Node(val);
if(!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
}
this.length++;
return this;
}
get(index) {
if(index < 0 || index >= this.length) return null;
if(index <= this.length/2) {
let count = 0;
let current = this.head;
while(count !== index) {
current = current.next;
count++;
}
return current;
} else {
let count = this.length - 1;
let current = this.tail;
while(count !== index) {
current = current.prev;
count--;
}
return current;
}
}
set(index, val) {
const foundNode = this.get(index);
if(foundNode !== null) {
foundNode.val = val;
return true;
}
return false;
}
insert(index, val) {
if(index < 0 || index > this.length) return false;
if(index === 0) return !!this.unshift(val);
if(index === this.length) return !!this.push(val);
const newNode = new Node(val);
const prevNode = this.get(index-1);
const nextNode = prevNode.next;
prevNode.next = newNode;
newNode.prev = prevNode;
newNode.next = nextNode;
nextNode.prev = newNode;
this.length++;
return true;
}
remove(index) {
if(index < 0 || index >= this.length) return undefined;
if(index === 0) return this.shift();
if(index === this.length-1) return this.pop();
const removedNode = this.get(index);
removedNode.prev.next = removedNode.next;
removedNode.next.prev = removedNode.prev;
removedNode.next = null;
removedNode.prev = null;
this.length--;
return removedNode;
}
}
const n = +require("fs").readFileSync("/dev/stdin").toString().trim();
const cards = new DoublyLinkedList();
for(let i = 1; i <= n; i++) {
cards.push(i);
}
const answer = [];
while(true) {
answer.push(cards.shift().val);
if(cards.length === 0)break;
cards.push(cards.shift().val);
}
console.log(answer.join(' '));
'BOJ > Node.js' 카테고리의 다른 글
[BOJ / Node.js] 4358. 생태학 (0) | 2023.03.28 |
---|---|
[BOJ / Node.js] 2841. 외계인의 기타 연주 (0) | 2023.03.27 |
[BOJ / Node.js] 17299. 오등큰수 (0) | 2023.03.24 |
[BOJ / Node.js] 2529. 부등호 (0) | 2023.03.23 |
[BOJ / Node.js] 17298. 오큰수 (0) | 2023.03.22 |