[algorithm] 백준 10828번 스택

2026. 1. 12. 19:59·algorithm/스택
반응형

 

스택 개념 이해 확인을 위한

기본 문제이다

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n;
    cin >> n;
    string s;

    stack<int> st;
    for(int tmp=0; tmp<n; tmp++)
        {
            cin >> s;
            if(s == "push")
            {
                int x;
                cin >> x;
                st.push(x);
            }

            if(s == "pop")
            {
                if(st.size() == 0) cout << -1;
                else
                {
                    cout << st.top();
                    st.pop();
                }
                cout << "\n";
            }

            if(s == "size")
            {
                cout << st.size();
                cout << "\n";
            }

            if(s == "empty")
            {
                if(st.size() == 0) cout << 1;
                else cout << 0;
                cout << "\n";
            }

            if(s == "top")
            {
                if(st.size() == 0) cout << -1;
                else
                {
                    cout << st.top();
                }
                cout << "\n";     
            }
            
        }
    
    return 0;
}

 

push X 의 입력을 구현하는 방식을 조금 고민했었는데

입력값이 "push"라면

그 때 X의 입력을 받는구조로 구현해주면 된다

반응형

'algorithm > 스택' 카테고리의 다른 글

[algorithm] 백준 3986번 좋은 단어  (0) 2026.01.25
[algorithm] 백준 9012번 괄호  (0) 2026.01.25
[algorithm] 백준 4949번 균형잡힌 세상  (0) 2026.01.25
[algorithm] 백준 10773번 제로  (0) 2026.01.12
'algorithm/스택' 카테고리의 다른 글
  • [algorithm] 백준 3986번 좋은 단어
  • [algorithm] 백준 9012번 괄호
  • [algorithm] 백준 4949번 균형잡힌 세상
  • [algorithm] 백준 10773번 제로
20puddle
20puddle
20puddle 님의 블로그 입니다.
  • 20puddle
    20puddle 님의 블로그
    20puddle
  • 전체
    오늘
    어제
    • 분류 전체보기 (100)
      • Spring boot (5)
      • git & github (5)
      • algorithm (47)
        • theory (3)
        • 배열 (7)
        • 연결 리스트 (3)
        • 스택 (5)
        • 큐 (3)
        • 덱 (2)
        • 기초 코드 (19)
        • BFS (5)
      • AI (43)
        • Machine Learning (35)
        • Deep Learning (8)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    ML
    Java
    연결리스트
    알고리즘
    주성분변환
    그레디언트 클리핑
    게시판
    Spring Boot
    Aimers
    list
    홀드아웃 검증
    머신러닝
    딥러닝
    백준
    DL
    github
    오프라인 학습
    AI
    인공지능
    git
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.3
20puddle
[algorithm] 백준 10828번 스택
상단으로

티스토리툴바