알고리즘/BOJ

백준 10866번 덱

꾸준함. 2018. 9. 12. 00:07

문제 링크입니다: https://www.acmicpc.net/problem/10866


STL 덱 활용을 연습하는 문제였습니다.


#include <iostream>

#include <string>

#include <deque>

using namespace std;

 

int main(void)

{

        ios_base::sync_with_stdio(0);

        cin.tie(0);

        int N;

        cin >> N;

 

        deque<int> dq;

       

        for (int i = 0; i < N; i++)

        {

                 string temp;

                 cin >> temp;

 

                 int num;

                 if (temp == "push_back")

                 {

                         cin >> num;

                         dq.push_back(num);

                 }

                 else if (temp == "push_front")

                 {

                         cin >> num;

                         dq.push_front(num);

                 }

                 else if (temp == "pop_front")

                 {

                         if (dq.empty())

                                 cout << -1 << "\n";

                         else

                         {

                                 cout << dq.front() << "\n";

                                 dq.pop_front();

                         }

                 }

                 else if (temp == "pop_back")

                 {

                         if (dq.empty())

                                 cout << -1 << "\n";

                         else

                         {

                                 cout << dq.back() << "\n";

                                 dq.pop_back();

                         }

                 }

                 else if (temp == "size")

                 {

                         cout << dq.size() << "\n";

                 }

                 else if (temp == "empty")

                 {

                         if (dq.empty())

                                 cout << 1 << "\n";

                         else

                                 cout << 0 << "\n";

                 }

                 else if (temp == "front")

                 {

                         if (dq.empty())

                                 cout << -1 << "\n";

                         else

                                 cout << dq.front() << "\n";

                 }

                 else if (temp == "back")

                 {

                         if (dq.empty())

                                 cout << -1 << "\n";

                         else

                                 cout << dq.back() << "\n";

                 }

        }

        return 0;

}


개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

백준 3988번 수 고르기  (0) 2018.09.12
문제 5430번 AC  (10) 2018.09.12
백준 15386번 Birokracija  (0) 2018.09.11
백준 1188번 음식 평론가  (0) 2018.09.11
백준 4179번 불  (2) 2018.09.10