728x90
반응형
문제 링크 : https://www.acmicpc.net/problem/28278
목차
1. 설명
2. 내 코드
3. 실행 결과
설명
백준 실버 4 문제 입니다.
문제명 : 스택 2
내 코드
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<string>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t, t1;
string str;
cin >> t;
stack<int> stack;
while (t--)
{
cin >> str;
if (str == "1")
{
cin >> t1;
stack.push(t1);
}
else if (str == "2")
{
if (!stack.empty())
{
cout << stack.top() << '\n';
stack.pop();
}
else
cout << -1 << '\n';
}
else if (str == "3")
{
cout << stack.size() << '\n';
}
else if (str == "4")
{
(stack.empty() ? cout << 1 << '\n' : cout << 0 << '\n');
}
else if (str == "5")
{
if (!stack.empty())
{
cout << stack.top() << '\n';
}
else
cout << -1 << '\n';
}
}
}
실행 결과

'c++' 카테고리의 다른 글
| 2025년도 2학기 게임 프로그래밍 팀 프로젝트 설명글 (0) | 2025.12.30 |
|---|---|
| c++의 function과 EventBus로 활용법 (0) | 2025.12.12 |
| 백준 c++ 1158 문제 풀이2 (0) | 2025.05.20 |
| 백준 c++ 1158 문제 풀이 (0) | 2025.05.19 |
| 백준 c++ 1966 문제 풀이 (0) | 2025.05.18 |