plmnj2003   2년 전

import java.util.Objects;
import java.util.Scanner;
import java.util.Stack;
public class main {
    public static void main(String []args){
        Stack<Integer> stack = new Stack<Integer>();
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int count =0;
        for(int i=0;i<N;i++) {
            String Command  = in.next();
            if(Command.equals("push")){
                    int data = in.nextInt();
                    stack.push(data);
                    count++;
            }
            if(Command.equals("pop")){
                if(stack.empty()==true){
                    System.out.printf("-1");
                }
                else{
                    System.out.println(stack.pop());
                    count--;
                }
            }
            if(Command.equals("empty")){
                if(stack.empty()==true){
                    System.out.printf("1");
                }
                else{
                    System.out.printf("0");
                }
            }
            if(Command.equals("top")){
                if(stack.empty()==true){
                    System.out.printf("-1");
                }else {
                    System.out.println(stack.peek());
                }
            }
            if(Command.equals("size")){
                System.out.println(count);
            }
        }
    }
}

occidere   2년 전

printf 사용하신부분에 줄바꿈을 빠뜨리신 것 같습니다.

System.out.printf("-1/n");

줄바꿈 모두 해주니 AC받네요

댓글을 작성하려면 로그인해야 합니다.