# 문제 출제 사이트

https://www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

# 문제

정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.

명령은 총 다섯 가지이다.

  • push X: 정수 X를 스택에 넣는 연산이다.
  • pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
  • size: 스택에 들어있는 정수의 개수를 출력한다.
  • empty: 스택이 비어있으면 1, 아니면 0을 출력한다.
  • top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.

 

# 입력

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 

둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다.

주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다.

문제에 나와있지 않은 명령이 주어지는 경우는 없다.

 

# 출력

출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.

 

 

# 제출한 소스코드

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 br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        Stack<Integer> stack = new Stack();
        for (int i = 0; i < n; i++) {
            String[] tempStrArr = br.readLine().split(" ");
            String command = tempStrArr[0];

            if (command.equals("push")) {
                int tempNum = Integer.parseInt(tempStrArr[1]);
                stack.push(tempNum);

            } else if(command.equals("top")) {
                if (!stack.empty()) System.out.println(stack.peek());
                else System.out.println("-1");

            } else if (command.equals("size")) {
                System.out.println(stack.size());

            } else if (command.equals("empty")) {
                if (stack.empty()) System.out.println("1");
                else System.out.println("0");

            } else if (command.equals("pop")) {
                if (stack.empty()) System.out.println("-1");
                else System.out.println(stack.pop());
            }
        }
    }
}
  • 스택과 조건문을 사용해서 쉽게 푼 방법
    => 스택에 있는 메서드들을 직접 구현해서 풀어보자

 

 

Stack 클래스 직접 구현해서 푼 것

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        customStack customStack = new customStack(n);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < n; i++) {
            String command = sc.next();


            switch (command) {

                case "push":
                    customStack.push(sc.nextInt());
                    break;

                case "top":
                    sb.append(customStack.peek()).append("\n");
                    break;

                case "size":
                    sb.append(customStack.size()).append("\n");
                    break;

                case "empty":
                    sb.append(customStack.empty()).append("\n");
                    break;

                case "pop":
                    sb.append(customStack.pop()).append("\n");
                    break;
            }
        }
        System.out.println(sb);
    }
}

class customStack {
    int[] stack;
    int size = 0;

    public customStack(int n) {
        stack = new int[n];
    }

    public void push(int num) {
        stack[size] = num;
        size++;
    }

    public int pop() {
        if (size == 0) {
            return -1;
        }

        int num = stack[size - 1];
        stack[size - 1] = 0;
        size--;
        return num;
    }

    public int size() {
        return size;
    }

    public int empty() {
        if (size == 0) return 1;
        else return 0;
    }

    public int peek() {
        if (size == 0) return - 1;
        return stack[size - 1];
    }
}
반응형

+ Recent posts