Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Eclipse
- CMD
- 2167. 2차원 배열의 합
- 20055
- 18222
- Error fetching remote repo 'origin'
- 백준
- to display the conditions report re-run your application with 'debug' enabled
- 14466
- 알고리즘
- 호석이두마리치킨
- 21278
- jenkins
- 투에모스문자열
- EC2
- docker
- 2108_통계학
- 설정
- 자바
- 별자리 만들기
- SpringBoot
- 프로그래머스
- 이클립스
- 소가길을건너간이유6
- 날짜일수
- Error
- 이산수학
- documentationpluginsbootstrapper
- Java
- dockercompose
Archives
- Today
- Total
계단을 오르듯이
[JAVA] 10773. 제로 본문
0 의 숫자가 입력으로 들어오면 가장 마지막에 받은 수를 지워야하므로 스택의 자료구조를 이용하였다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package algo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class B_10773_제로 {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(in.readLine());
Stack<Integer> stack = new Stack<>(); // 뒤 숫자부터 제거하기 위해 스택 사용
for (int i = 0; i < N; i++) {
int n = Integer.parseInt(in.readLine());
if (n != 0) { // 0 이 아니면 push
stack.push(n);
} else // 0 이면 pop
stack.pop();
}
long result = 0; // long
while (!stack.isEmpty()) { // 스택에 존재하는 수를 더한다.
result += stack.pop();
}
System.out.println(result);
}
}
|
cs |
'알고리즘 > 백준_JAVA' 카테고리의 다른 글
[JAVA] 11403. 경로 찾기 (0) | 2022.02.06 |
---|---|
[JAVA] 2638. 치즈 (0) | 2022.02.06 |
[JAVA] 1316. 그룹 단어 체커 (0) | 2022.02.04 |
[JAVA] 7569. 토마토 (0) | 2022.02.03 |
[JAVA] 6443. 애너그램 (0) | 2022.02.02 |