[백준/C++] 18258번 큐 2
2021. 3. 21. 17:09ㆍ알고리즘/구현
728x90
반응형
문제: www.acmicpc.net/problem/18258
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
문제풀이
큐의 메소드들을 구현하는 문제이다.
명령의 수 N의 범위가 1 ~ 2,000,000 이므로 큐의 크기를 2,000,000으로 선언했다.
코드
#include <iostream>
using namespace std;
void push(int);
int pop();
int front();
int back();
int size();
int empty();
int fir = 0, rear = 0;
int que[2000000];
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int N;
cin>>N;
while(N--){
string oper;
cin>>oper;
if(oper == "push"){
int num;
cin>>num;
push(num);
}
else if(oper == "pop"){
cout<<pop()<<"\n";
}
else if(oper == "front"){
cout<<front()<<"\n";
}
else if(oper == "back"){
cout<<back()<<"\n";
}
else if(oper == "size"){
cout<<size()<<"\n";
}
else if(oper == "empty"){
cout<<empty()<<"\n";
}
}
return 0;
}
void push(int num){
que[rear++] = num;
}
int pop(){
if(empty())
return -1;
else{
return que[fir++];
}
}
int front(){
if(empty())
return -1;
else
return que[fir];
}
int back(){
if(empty())
return -1;
else
return que[rear-1];
}
int size(){
return rear - fir;
}
int empty(){
if(fir == rear)
return 1;
else
return 0;
}
결과
728x90
반응형
'알고리즘 > 구현' 카테고리의 다른 글
[백준/C++] 2504번 괄호의 값 (0) | 2021.03.23 |
---|---|
[백준/C++] 5430번 AC (0) | 2021.03.22 |
[백준/C++] 1021번 회전하는 큐 (0) | 2021.03.21 |
[백준/C++] 2493번 탑 (0) | 2021.03.20 |
[백준/C++] 5397번 키로거 (0) | 2021.03.19 |