Posts [BOJ 백준] 10845번 큐 / C++
Post
Cancel

[BOJ 백준] 10845번 큐 / C++


Contents



문제


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


풀이


STL queue를 사용하여 풀었습니다.

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


소스 코드


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
56
57
58
59
60
61
62
63
#include <iostream>
#include <queue>
#include <string>

using namespace std;

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

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



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

[WebLogic] 웹로직 콘솔 특정 ip 접속 제한 방법

[WebLogic] 웹로직 버전 확인 방법

Comments powered by Disqus.