[BOJ / C++] 10828 스택

Seulguo·2022년 7월 12일
0

Algorithm

목록 보기
43/185
post-thumbnail
post-custom-banner

🐣 문제

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


🐥 코드

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


int main(){	

  int n = 0;
  cin >> n;
  vector<int> stack;
  
  for(int i = 0; i < n; i++){
    string s; 
    cin >> s;
   
    if(s == "push"){
      int num = 0;
      cin >> num;
      stack.push_back(num);
    }
    else if(s == "pop"){
      if(stack.empty()) cout << "-1" << endl;
      else{
        cout << stack.back() << endl;
        stack.pop_back();
      }
    }
    else if(s == "size"){
      cout << stack.size() << endl;
    }
    else if(s == "empty"){
      if(stack.empty()) cout << "1" << endl;
      else cout << "0" << endl;
    }
    else if(s == "top"){
      if(stack.empty()) cout << "-1" << endl;
      else cout << stack.back() << endl;
    }
  }
  
  return 0;
}
post-custom-banner

0개의 댓글