Skip to content

Commit 51998e6

Browse files
committed
[Silver IV] Title: 스택 2, Time: 2472 ms, Memory: 340736 KB -BaekjoonHub
1 parent 0ce73e3 commit 51998e6

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [Silver IV] 스택 2 - 28278
2+
3+
[문제 링크](https://www.acmicpc.net/problem/28278)
4+
5+
### 성능 요약
6+
7+
메모리: 340736 KB, 시간: 2472 ms
8+
9+
### 분류
10+
11+
자료 구조, 스택
12+
13+
### 제출 일자
14+
15+
2024년 3월 31일 22:11:45
16+
17+
### 문제 설명
18+
19+
<p>정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.</p>
20+
21+
<p>명령은 총 다섯 가지이다.</p>
22+
23+
<ol>
24+
<li><span style="color:#e74c3c;"><code>1 X</code></span>: 정수 <var>X</var>를 스택에 넣는다. (1 ≤ <var>X</var> ≤ 100,000)</li>
25+
<li><span style="color:#e74c3c;"><code>2</code></span>: 스택에 정수가 있다면 맨 위의 정수를 빼고 출력한다. 없다면 <span style="color:#e74c3c;"><code>-1</code></span>을 대신 출력한다.</li>
26+
<li><span style="color:#e74c3c;"><code>3</code></span>: 스택에 들어있는 정수의 개수를 출력한다.</li>
27+
<li><span style="color:#e74c3c;"><code>4</code></span>: 스택이 비어있으면 <span style="color:#e74c3c;"><code>1</code></span>, 아니면 <span style="color:#e74c3c;"><code>0</code></span>을 출력한다.</li>
28+
<li><span style="color:#e74c3c;"><code>5</code></span>: 스택에 정수가 있다면 맨 위의 정수를 출력한다. 없다면 <span style="color:#e74c3c;"><code>-1</code></span>을 대신 출력한다.</li>
29+
</ol>
30+
31+
### 입력
32+
33+
<p>첫째 줄에 명령의 수 <var>N</var>이 주어진다. (1 ≤ <var>N</var> ≤ 1,000,000)</p>
34+
35+
<p>둘째 줄부터 <var>N</var>개 줄에 명령이 하나씩 주어진다.</p>
36+
37+
<p>출력을 요구하는 명령은 하나 이상 주어진다.</p>
38+
39+
### 출력
40+
41+
<p>출력을 요구하는 명령이 주어질 때마다 명령의 결과를 한 줄에 하나씩 출력한다.</p>
42+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class Main {
4+
public static void main(String[] args) throws IOException {
5+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
7+
Stack<Integer> stk = new Stack<>();
8+
br.lines().takeWhile(l -> !l.isEmpty()).skip(1).mapToInt(r -> {
9+
int[] arr = Arrays.stream(r.split(" ")).mapToInt(Integer::parseInt).toArray();
10+
switch (arr[0]) {
11+
case 2:
12+
return !stk.empty() ? stk.pop() : -1;
13+
case 3:
14+
return stk.size();
15+
case 4:
16+
return !stk.empty() ? 0 : 1;
17+
case 5:
18+
return !stk.empty() ? stk.peek() : -1;
19+
case 1:
20+
stk.push(arr[1]);
21+
}
22+
return -2;
23+
}).forEach(r -> {
24+
if (r != -2) {
25+
try {
26+
bw.write(r + "\n");
27+
} catch (IOException e) {}
28+
}
29+
});
30+
bw.flush();
31+
bw.close();
32+
}
33+
}

0 commit comments

Comments
 (0)