Study📚/백준
[백준] - #10828: 스택
woo!na
2024. 6. 2. 01:40
문제 출처 : https://www.acmicpc.net/
#10828: 스택 (언어 : Java11)
제출 답안
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
static Stack<Integer> stack = new Stack<Integer>();
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int interval = Integer.parseInt(br.readLine());
for(int i=0; i<interval; i++) {
String command = br.readLine();
switch (command) {
case "top": System.out.println(myTop());
break;
case "empty": System.out.println(myEmpty());
break;
case "size" : System.out.println(mySize());
break;
case "pop" : System.out.println(myPop());
break;
default : myPush(Integer.parseInt(command.substring(command.indexOf(" ")+1)));
}
}
br.close();
}
public static void myPush(int num) {
stack.push(num);
}
public static int myPop() {
int result = 0;
if(stack.empty())
result = -1;
else
result = stack.pop();
return result;
}
public static int mySize() {
int result = stack.size();
return result;
}
public static int myEmpty() {
int result = 0;
if(stack.empty())
result = 1;
else
result = 0;
return result;
}
public static int myTop() {
int result = 0;
if(stack.empty())
result = -1;
else
result = stack.peek();
return result;
}
}
comment
- 메소드 굳이굳이 분리한 이유... 안 하면 내가 안 읽혀서... 쩝스바리