-
10773 - 제로알고리즘 2023. 5. 24. 22:27728x90
https://www.acmicpc.net/problem/10773
10773번: 제로
첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경
www.acmicpc.net
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Stack<Integer> stack = new Stack<>(); // 받을 숫자 갯수 int rows = Integer.parseInt(reader.readLine()); // rows만큼 반복 for (int i = 0; i < rows; i++) { int num = Integer.parseInt(reader.readLine()); if (num == 0) { stack.pop(); continue; } stack.push(num); } int sum = 0; for(int a : stack){ sum+=a; } System.out.println(sum); } }
느낀점
stack 자료구조 활용해서 해본 것이 재밌었다.
스택은 기본적으로 FILO(First In Last Out) 구조였고, javaDoc을 보면서
기존의 List와 CRUD는 비슷하겠거니,하면서 접근해봤는데 재밌었다.
'알고리즘' 카테고리의 다른 글
1874 - 스택 수열 (1) 2023.05.26 18258 - 큐2 (0) 2023.05.24 1002 - 터렛 (0) 2023.05.24 1011 - Fly me to the Alpha Centauri (0) 2023.05.24 1110 - 더하기 사이클 (2) 2023.05.24