Recent Posts
Notice
No Rules Rules
스택 (feat. 백준, 10828번) 본문
728x90
반응형
스택
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <stack>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
register int N;
string str;
stack<int> ans;
cin >> N;
for (register int n = 0, v; n < N; ++n) {
cin >> str;
if (!str.compare("push"))
cin >> v, ans.push(v);
else if (!str.compare("pop"))
if (ans.empty())
cout << -1 << "\n";
else
cout << ans.top() << "\n", ans.pop();
else if (!str.compare("size"))
cout << ans.size() << "\n";
else if (!str.compare("empty"))
if (ans.empty())
cout << 1 << "\n";
else
cout << 0 << "\n";
else
if (ans.empty())
cout << -1 << "\n";
else
cout << ans.top() << "\n";
}
return 0;
}
// *&)*@*
자료구조 stack을 사용하여 문제에 주어진 문자열에 따라 출력하는 문제입니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
괄호 (feat. 백준, 9012번) (0) | 2022.08.11 |
---|---|
제로 (feat. 백준, 10773번) (0) | 2022.08.11 |
주유소 (feat. 백준, 13305번) (0) | 2022.08.11 |
그룹 단어 체커 (feat. 백준, 1316번) (0) | 2022.08.11 |
크로아티아 알파벳 (feat. 백준, 2941번) (0) | 2022.08.11 |
Comments