๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 10828๋ฒˆ - ์Šคํƒ

by dkswnkk 2021. 10. 25.

 

 

10828๋ฒˆ: ์Šคํƒ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    stack<int>st;

    int N; cin >> N;
    while (N--) {
        string cmd; cin >> cmd;
        if (cmd == "push") {
            int num; cin >> num;
            st.push(num);
        }
        else if (cmd == "top") {
            if (st.empty()) cout << -1<<"\n";
            else cout << st.top()<<"\n";
        }
        else if (cmd == "size") cout << st.size()<<"\n";
        else if (cmd == "pop") {
            if (st.empty()) cout << -1 << "\n";
            else {
                cout << st.top()<<"\n";
                st.pop();
            }
        }
        else if (cmd == "empty") {
            if (st.empty()) cout << 1 << "\n";
            else cout << 0 << "\n";
        }
    }

}

๋Œ“๊ธ€