Skip to content

Commit b33229e

Browse files
committed
[Silver IV] Title: 카드2, Time: 196 ms, Memory: 41304 KB -BaekjoonHub
1 parent 79c3dc3 commit b33229e

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Silver IV] 카드2 - 2164
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2164)
4+
5+
### 성능 요약
6+
7+
메모리: 41304 KB, 시간: 196 ms
8+
9+
### 분류
10+
11+
자료 구조, 큐
12+
13+
### 제출 일자
14+
15+
2026년 2월 28일 16:31:50
16+
17+
### 문제 설명
18+
19+
<p>N장의 카드가 있다. 각각의 카드는 차례로 1부터 N까지의 번호가 붙어 있으며, 1번 카드가 제일 위에, N번 카드가 제일 아래인 상태로 순서대로 카드가 놓여 있다.</p>
20+
21+
<p>이제 다음과 같은 동작을 카드가 한 장 남을 때까지 반복하게 된다. 우선, 제일 위에 있는 카드를 바닥에 버린다. 그 다음, 제일 위에 있는 카드를 제일 아래에 있는 카드 밑으로 옮긴다.</p>
22+
23+
<p>예를 들어 N=4인 경우를 생각해 보자. 카드는 제일 위에서부터 1234 의 순서로 놓여있다. 1을 버리면 234가 남는다. 여기서 2를 제일 아래로 옮기면 342가 된다. 3을 버리면 42가 되고, 4를 밑으로 옮기면 24가 된다. 마지막으로 2를 버리고 나면, 남는 카드는 4가 된다.</p>
24+
25+
<p>N이 주어졌을 때, 제일 마지막에 남게 되는 카드를 구하는 프로그램을 작성하시오.</p>
26+
27+
### 입력
28+
29+
<p>첫째 줄에 정수 N(1 ≤ N ≤ 500,000)이 주어진다.</p>
30+
31+
### 출력
32+
33+
<p>첫째 줄에 남게 되는 카드의 번호를 출력한다.</p>
34+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const input = fs
5+
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
6+
.toString()
7+
.trim()
8+
.split("\n");
9+
10+
const N = +input[0];
11+
const cardArr = [];
12+
for (let i = 1; i <= N; i++) {
13+
cardArr.push(i);
14+
}
15+
16+
function SortCard(arr, n) {
17+
let front = 0;
18+
19+
while (arr.length - front > 1) {
20+
front++;
21+
if (arr.length - front === 1) {
22+
break;
23+
}
24+
arr.push(arr[front]);
25+
front++;
26+
}
27+
return arr[front];
28+
}
29+
30+
console.log(SortCard(cardArr, N));

0 commit comments

Comments
 (0)