[c++]백준 2164번: 카드2 (원형 큐 구현 & 군수열)

2022. 6. 24. 00:01·problem solving/백준

문제

https://www.acmicpc.net/problem/2164

 

풀이

 

🚩원형 큐

앞에 카드를 빼고 뒤로 넣는다? -> 선입선출 -> queue 문제구나.

 

라이브러리를 사용하면 쉽게 풀 수 있고, 클래스로 직접 구현해서 풀어도 된다. 

나는 겸사겸사 queue 라이브러리를 꼼꼼히 볼겸 간단하게 직접 구현해서 풀었다. 

 

Queue 라이브러리의 내부 코드가 궁금하면 아래 사이트를 참조하면 된다.

https://en.cppreference.com/w/cpp/container/queue

 

🚩군수열

서치 하다가 군수열로 규칙찾는 방법도 있더라..  근데 난 못 알아들음 (〃⌒▽⌒〃)ゝ  

https://hoho325.tistory.com/138

 

소스

#include <iostream>
#define MAX_SIZE 500001

class Queue {
    
public:
    int count;
    
    Queue(int n): size(n), front(-1), rear(-1), count(0){};
    
    bool isFull()
    {
        if ((front == 0 && rear == size - 1) || (front == rear + 1))
            return true;
        else
            return false;
    }
    
    bool isEmpty()
    {
        if (front == -1)
            return true;
        else
            return false;
    }
    
    void enQueue(int data)
    {
        if (!isFull())
        {
            if (front == -1) front = 0;
            rear = (rear + 1) % size;
            items[rear] = data;
            ++count;
        }
    }
    
    int deQueue()
    {
        if (isEmpty())
            return (-1);
        
        int data = items[front];
        if (front == rear)
        {
            front = -1;
            rear = -1;
        }
        else
        {
            front = (front + 1) % size;
        }
        --count;
        return data;
    }
    
    void display()
    {
        std::cout << "\ndisplay---------------\n";

        for(int i = 0; i<size; ++i)
        {
            std::cout << items[i] << "\t";
        }
        std::cout << "\n----------------------\n";
    }
    
private:
    int items[MAX_SIZE];
    int front, rear;
    int size;
    
    
};

int main()
{
    int N = 0;
    std::cin >> N;
    if(N==1)
    {
        std::cout << N << std::endl;
        return 0;
    }
    
    Queue q(N);
    q.deQueue();
    for(int i = 1; i<=N; ++i)
    {
        q.enQueue(i);
    }
    while(1)
    {
        if(q.count == 1)
        {
            std::cout << q.deQueue() << std::endl;
            break;
        }
        q.deQueue();
        q.enQueue(q.deQueue());

    }
    
    return 0;
}

 

저작자표시 비영리 변경금지 (새창열림)
'problem solving/백준' 카테고리의 다른 글
  • [C++] 백준 10828번: 스택
  • [C++] 백준 1966번: 프린터 큐
  • [C++] 백준 16500번: 문자열 판별
  • [C++] 백준 13458번 : 시험감독
u1qns
u1qns
그냥 알고리즘 풀이만 올리려고 했는데, 정보 찾다보니까 너무 답답해서 이것저것 쓰다보니 커졌어요.
  • u1qns
    블로그
    u1qns
  • 전체
    오늘
    어제
    • 분류 전체보기 (171)
      • 회고 (1)
      • programming (17)
        • 개념 정리 (6)
        • CI CD (1)
        • 트러블 슈팅 (0)
        • 환경설정 및 팁 (7)
      • problem solving (153)
        • 개념 정리 (3)
        • 백준 (127)
        • SWEA (15)
        • 프로그래머스 (4)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    SSAFY수료식
    cmath
    그리디
    투포인터
    C++
    되추적
    미해결
    POW
    DP
    백준
    HELLOSSAFY
    BFS
    SSAFY
    SWEA
    삼성청년SW아카데미
    완전탐색
    cpp
    boj
    구현
    DFS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
u1qns
[c++]백준 2164번: 카드2 (원형 큐 구현 & 군수열)
상단으로

티스토리툴바