Posts [BOJ 백준] 10828번 스택 / C++
Post
Cancel

[BOJ 백준] 10828번 스택 / C++


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


STL stack을 사용하여 풀었습니다.

스택이 비어 있을 때, pop(), top() 함수 호출을 하면 런타임 에러가 발생할 수 있으므로 그 부분을 주의해 풀었습니다.


소스 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main(int argc, char**argv) {

	ios::sync_with_stdio(0);
	cin.tie(0);
		
	stack<int> S;
	int N;
	
	cin >> N;
	
	while(N--){
		string str;
		cin >> str;
		
		if(str=="push"){
			int X;
			cin >> X;
			S.push(X);
		}
		
		else if(str=="pop"){
			if(!S.empty()){
				cout << S.top() << "\n";
				S.pop();
			}
			else
				cout << "-1\n";
		}
		
		else if(str=="size"){
			cout << S.size() << "\n";
		}
		
		else if(str=="empty"){
			cout << S.empty() << "\n";
		}
		
		else if(str=="top"){
			if(!S.empty()){
				cout << S.top() << "\n";	
			}
			else
				cout << "-1\n";
		}
		
	}
	
	return 0;
}



This post is licensed under CC BY 4.0 by the author.

[DB 데이터베이스] 무결성 제약조건 (Integrity Constraint)

[BOJ 백준] 10773번 제로 / C++

Comments powered by Disqus.