백준 2164 - 카드2

황재진·2024년 3월 11일

백준

목록 보기
22/54
post-thumbnail

원형 큐에 대해 이해하고 있으면 쉽게 풀 수 있는 문제입니다.

링크에 원형 큐에 대한 설명이 잘 작성되어있습니다.

#include <iostream>

int main()
{
	int n;
	std::cin >> n;

	int* queue = new int[n];
	for (int i = 0; i < n; i++)
		queue[i] = -1;

	int front = 0;
	int rear = -1;

	for (int i = 0; i < n; i++)
	{
		queue[(rear + 1) % n] = i;
		rear++;
	}

	int t = 0;
	while (rear - front != 0)
	{
		if (t % 2 == 0)
		{
			queue[(front) % n] = -1;
			front++;
		}
		else
		{
			int temp = queue[(front) % n];
			queue[(rear + 1) % n] = temp;
			queue[(front) % n] = -1;
			front++;
			rear++;
		}
		t++;
	}

	std::cout << queue[(rear) % n] + 1;
}
profile
프로그래밍, 쉐이더 등 이것저것 다해보는 게임 개발자입니다

0개의 댓글