곽로그
[백준 10828] 스택 본문
반응형
https://www.acmicpc.net/problem/10828
문제
기본문제. 명령어를 입력받을 때 if를 쓰거나 swich문을 쓰거나
import java.util.Scanner;
class stack{
public static int[] stack=new int[10000];
public static int pointer=-1;
public static void push(int n) {
if(pointer>=stack.length)
System.out.println("overflow");
else
stack[++pointer]=n;
}
public static int pop() {
if(pointer==-1) {
return -1;
}
else
return stack[pointer--];
}
public static int size() {
return pointer+1;
}
public static int empty() {
if(pointer==-1)
return 1;
else
return 0;
}
public static int top() {
if(pointer==-1) {
return -1;
}
else
return stack[pointer];
}
}
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int N=in.nextInt();
for(int i=0;i<N;i++) {
String c=in.next();
switch(c) {
case "push":
int n=in.nextInt();
stack.push(n);
break;
case "pop":
System.out.println(stack.pop());
break;
case "empty":
System.out.println(stack.empty());
break;
case "size":
System.out.println(stack.size());
break;
case "top":
System.out.println(stack.top());
break;
}
}
}
}
package stack;
import java.util.ArrayList;
import java.util.Scanner;
class Stack{
int[] stack;
int pointer;
public Stack(int num) {
stack=new int[num];
this.pointer=-1;
}
public void push(int num) {
pointer++;
stack[pointer]=num;
}
public int pop() {
if(pointer<0) {
return -1;
}
return stack[pointer--];
}
public int size() {
return pointer+1;
}
public int empty() {
if(pointer<0) {
return 1;
}
else {
return 0;
}
}
public int top() {
if(pointer<0) {
return -1;
}
return stack[pointer];
}
}
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int N=in.nextInt();
Stack stack=new Stack(10000);
for(int i=0;i<N;i++) {
String command=in.next();
if(command.equals("push")) {
int num=in.nextInt();
stack.push(num);
}
else if(command.equals("pop")) {
System.out.println(stack.pop());
}
else if(command.equals("size")) {
System.out.println(stack.size());
}
else if(command.equals("empty")) {
System.out.println(stack.empty());
}
else if(command.equals("top")) {
System.out.println(stack.top());
}
else {
System.out.println("다시풀어라~~");
}
}
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준 10845] 큐 (0) | 2020.01.04 |
---|---|
[백준 9012] 괄호 (0) | 2019.12.29 |
[백준 1874] 스택수열 (0) | 2019.12.27 |
[백준 2841] 외계인의 기타연주 -보완필요 (0) | 2019.12.27 |
[백준 10773] 제로 (0) | 2019.12.27 |
Comments