View

반응형

 

알고리즘 분류: 자료 구조, 큐

문제 링크: https://www.acmicpc.net/problem/18258

 

18258번: 큐 2

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

 

【 풀이 】

 

2023.04.09 - [백준 [BAEKJOON]] - [백준] 10845번: 큐 [C++]

 

[백준] 10845번: 큐 [C++]

알고리즘 분류: 자료 구조, 큐 문제 링크: https://www.acmicpc.net/problem/10845 10845번: 큐 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다.

baehoon.tistory.com

10845번: 큐 문제의 풀이에서 개행을 '\n'으로 해주고 빠른 입출력을 도와주는 문장을 삽입하면 된다.

endl  ==>  '\n' 


//	밑의 세줄을 main 함수에 추가해 빠른 입출력.	//

ios_base::sync_with_stdio(false);		
cin.tie(NULL);
cout.tie(NULL);

 

 

【 코드 】

 

#include<iostream>
#include<string>
#include<queue>
using namespace std;

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	queue<int>q;
	int n;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		string command;
		cin >> command;

		if (command == "push")
		{
			int num;
			cin >> num;
			q.push(num);
		}
		else if (command == "pop")
		{
			if (q.empty())
				cout << -1 << '\n';
			else
			{
				cout << q.front() << '\n';
				q.pop();
			}
		}
		else if (command == "size")
		{
			cout << q.size() << '\n';
		}
		else if (command == "empty")
		{
			cout << q.empty() << '\n';
		}
		else if (command == "front")
		{
			if (q.empty())
				cout << -1 << '\n';
			else
				cout << q.front() << '\n';
		}
		else if (command == "back")
		{
			if (q.empty())
				cout << -1 << '\n';
			else
				cout << q.back() << '\n';
		}
	}
	return 0;
}

 

728x90
반응형
Share Link
reply
250x250
반응형
«   2024/10   »
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