본문 바로가기

Problem Solving/백준

[백준/C++] 10828번 스택


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


int main() {
    int N, temp;
    cin >> N;
    string action;
    vector<int> arr;
    for(int i = 0; i < N; i++) {
        cin >> action;
        if(action == "push") {
            cin >> temp;
            arr.push_back(temp);
        } else if(action == "top") {
            arr.empty() ? cout << -1 << endl : cout << arr.back() << endl;
        } else if(action == "size") {
            cout << arr.size() << endl;
        } else if(action == "empty") {
            cout << arr.empty() << endl;
        } else {
            if(arr.empty()) cout << -1 << endl;
            else {
                cout << arr.back() << endl;
                arr.pop_back();
            }
        }
    }
}

dd


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

 

10828번: 스택

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

www.acmicpc.net