Problem Solving/백준

[백준-10828] 스택

<문제>


<Code>

#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {
  int n;
  cin>>n;

  stack<int> st; //int type의 stack 선언.
  string str;
  for(int i=0; i<n; i++){
    cin>>str;
    if(str=="push"){ //push 명령어 일때
      int num;
      cin>>num;
      st.push(num);
    }else if(str=="pop"){
      if(!st.empty()){
        cout<<st.top()<<endl;
        st.pop();
      }else{
        cout<< "-1"<<endl;
      }
    }else if(str=="size"){
      cout<<st.size()<<endl;
    }else if(str=="empty"){
      if(st.empty()){
        cout<<"1"<<endl;
      }else {
        cout<<"0"<<endl;
      }
    }else if(str=="top"){
      if(!st.empty()){
        cout<<st.top()<<endl;
      }else{
        cout<<"-1"<<endl;
      }
    }
  }
    return 0;
  }


<Comment>

#include <stack>의 소중함^^ C로 구현할 생각하니 아찔


www.acmicpc.net/problem/10828

 

10828번: 스택

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

www.acmicpc.net

 

'Problem Solving > 백준' 카테고리의 다른 글

[백준-1158] 요세푸스 문제  (0) 2021.03.22
[백준-10845] 큐  (0) 2021.03.17
[백준-9012] 괄호  (0) 2021.02.05
[백준-15596] 정수 N개의 합  (0) 2021.02.05
[백준-2562] 최댓값  (0) 2021.02.04