본문 바로가기

[BOJ] 10828번 스택

@myeongsang2025. 4. 30. 12:04
반응형

1.문제😂

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

#!/usr/bin/bash

echo "이 문제는 스택에 관한문제이다."
echo "해당 push, pop, size, empty, top 을 입력 받아 출력하는 스택의 기본 문제이다."
echo "c++ 코드를 작성하였으며, std::stack을 이용하여 쉽게 풀 수 있다."

2.코드🐱‍🚀

#include <bits/stdc++.h>

using namespace std;

int main(void){
    ios::sync_with_stdio();
    cin.tie(0); cout.tie(0);
    
    stack<int> S;
    
    int num;
    cin >> num;
    while(num--){
        string txt_in;
        cin >> txt_in;
        
        if(txt_in == "push"){
            int push_input;
            cin >> push_input;
            S.push(push_input);
        }else if(txt_in == "pop"){
            if(S.empty()){ cout << -1 << '\n'; }
            else{ 
                cout << S.top() << '\n'; 
                S.pop();
            }
        }else if(txt_in == "size"){
            cout << S.size() << '\n';
        }else if(txt_in == "empty"){
            cout << (int)S.empty() << '\n';
        }else {
            if(S.empty()) cout << -1 << '\n';
            else cout << S.top() << '\n';
        }
    }
    
    
    
}

 

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

[BOJ] 10845번 큐  (0) 2025.05.02
[BOJ] 10773번 제로  (0) 2025.04.30
[BOJ] 10807번 개수 세기  (0) 2024.10.24
[BOJ] 2490번 윷놀이  (0) 2024.10.05
[BOJ] 2480번 주사위 세개  (0) 2024.10.05
myeongsang
@myeongsang :: myeongsang

myeongsang's security

공감하셨다면 ❤️ 구독도 환영합니다! 🤗

목차